Esempio n. 1
0
        static bool AreSame(TypeReference a, TypeReference b)
        {
            if (a is TypeSpecification || b is TypeSpecification)
            {
                if (a.GetType() != b.GetType())
                {
                    return(false);
                }

                return(AreSame((TypeSpecification)a, (TypeSpecification)b));
            }

            if (a is GenericParameter || b is GenericParameter)
            {
                if (a.GetType() != b.GetType())
                {
                    return(false);
                }

                return(AreSame((GenericParameter)a, (GenericParameter)b));
            }

            return(a.FullName == b.FullName);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public static bool AreSame(TypeReference a, TypeReference b)
        {
            if (a.IsGenericParameter || b.IsGenericParameter)
            {
                return true;
            }
                //return AreSame((GenericParameter)a, (GenericParameter)b);

            if (a.MetadataType != b.MetadataType)
               return false;

            if (a is TypeSpecification || b is TypeSpecification)
            {
                if (a.GetType() != b.GetType())
                    return false;

                return AreSame((TypeSpecification)a, (TypeSpecification)b);
            }

            return a.FullName == b.FullName;
        }
        public static bool TypeMatch(TypeReference a, TypeReference b)
        {
            if (a is GenericParameter)
                return true;

            if (a is TypeSpecification || b is TypeSpecification) {
                if (a.GetType () != b.GetType ())
                    return false;

                return TypeMatch ((TypeSpecification) a, (TypeSpecification) b);
            }

            return a.FullName == b.FullName;
        }
Esempio n. 5
0
		static bool TypeMatch (TypeReference a, TypeReference b, ref Dictionary<string,string> gp)
		{
			var gpa = a as GenericParameter;
			if (gpa != null) {
				if (gp == null)
					gp = new Dictionary<string, string> ();
				string match;
				if (!gp.TryGetValue (gpa.FullName, out match)) {
					// first use, we assume it will always be used this way
					gp.Add (gpa.FullName, b.ToString ());
					return true;
				}
				// re-use, it should match the previous usage
				return match == b.ToString ();
			}

			if (a is TypeSpecification || b is TypeSpecification) {
				if (a.GetType () != b.GetType ())
					return false;

				return TypeMatch ((TypeSpecification) a, (TypeSpecification) b, ref gp);
			}

			return a.FullName == b.FullName;
		}
Esempio n. 6
0
        static bool TypeMatch(TypeReference a, TypeReference b)
        {
            if (a is GenericParameter)
                return true; // not exact, but a guess is enough for us

            if (a is TypeSpecification || b is TypeSpecification)
            {
                if (a.GetType() != b.GetType())
                    return false;

                return TypeMatch((TypeSpecification)a, (TypeSpecification)b);
            }

            return a.FullName == b.FullName;
        }
        public MemberReference GetMemberRefAt(uint rid, GenericContext context)
        {
            int             index  = (int)rid - 1;
            MemberReference member = m_memberRefs [rid - 1];

            if (member != null)
            {
                return(member);
            }

            MemberRefTable mrTable = m_tableReader.GetMemberRefTable();
            MemberRefRow   mrefRow = mrTable [index];

            Signature sig = m_sigReader.GetMemberRefSig(mrefRow.Class.TokenType, mrefRow.Signature);

            switch (mrefRow.Class.TokenType)
            {
            case TokenType.TypeDef:
            case TokenType.TypeRef:
            case TokenType.TypeSpec:
                TypeReference  declaringType = GetTypeDefOrRef(mrefRow.Class, context);
                GenericContext nc            = context.Clone();

                if (declaringType is GenericInstanceType)
                {
                    TypeReference ct = declaringType;
                    while (ct is GenericInstanceType)
                    {
                        ct = (ct as GenericInstanceType).ElementType;
                    }

                    nc.Type          = ct;
                    nc.AllowCreation = ct.GetType() == typeof(TypeReference);
                }

                if (sig is FieldSig)
                {
                    FieldSig fs = sig as FieldSig;
                    member = new FieldReference(
                        m_root.Streams.StringsHeap [mrefRow.Name],
                        declaringType,
                        GetTypeRefFromSig(fs.Type, nc));
                }
                else
                {
                    string    name = m_root.Streams.StringsHeap [mrefRow.Name];
                    MethodSig ms   = sig as MethodSig;

                    MethodReference methref = new MethodReference(
                        name, ms.HasThis, ms.ExplicitThis, ms.MethCallConv);
                    methref.DeclaringType = declaringType;

                    if (sig is MethodDefSig)
                    {
                        int arity = (sig as MethodDefSig).GenericParameterCount;
                        for (int i = 0; i < arity; i++)
                        {
                            methref.GenericParameters.Add(new GenericParameter(i, methref));
                        }
                    }

                    nc.Method = methref;

                    methref.ReturnType = GetMethodReturnType(ms, nc);

                    methref.ReturnType.Method = methref;
                    for (int j = 0; j < ms.ParamCount; j++)
                    {
                        Param p = ms.Parameters [j];
                        ParameterDefinition pdef = BuildParameterDefinition(
                            string.Concat("A_", j), j, new ParamAttributes(), p, nc);
                        pdef.Method = methref;
                        methref.Parameters.Add(pdef);
                    }
                    member    = methref;
                    nc.Method = methref;
                }
                break;

            case TokenType.Method:
                // really not sure about this
                MethodDefinition methdef = GetMethodDefAt(mrefRow.Class.RID);
                member = new MethodReference(
                    methdef.Name, methdef.HasThis,
                    methdef.ExplicitThis, methdef.CallingConvention);
                member.DeclaringType = methdef.DeclaringType;
                break;

            case TokenType.ModuleRef:
                break;                 // TODO, implement that, or not
            }

            member.MetadataToken = MetadataToken.FromMetadataRow(TokenType.MemberRef, index);
            m_module.MemberReferences.Add(member);
            m_memberRefs [index] = member;

            return(member);
        }
Esempio n. 8
0
        // a must be exactly a TypeReference or a TypeDefinition
        static int typeReferenceHashCode(TypeReference a)
        {
            if (a == null)
                return 0;

            if (a.GetType() != typeof(TypeReference) && a.GetType() != typeof(TypeDefinition))
                throw new ApplicationException("arg must be exactly of type TypeReference or TypeDefinition");

            int res = 0;

            res += a.Name.GetHashCode();
            res += a.Namespace.GetHashCode();
            res += typeHashCode(a.DeclaringType);
            res += scopeHashCode(a.Scope);

            return res;
        }
Esempio n. 9
0
        // a and b must be either exactly a TypeReference or exactly a TypeDefinition
        static bool compareTypeReferences(TypeReference a, TypeReference b)
        {
            if ((a.GetType() != typeof(TypeReference) && a.GetType() != typeof(TypeDefinition)) ||
                (b.GetType() != typeof(TypeReference) && b.GetType() != typeof(TypeDefinition)))
                throw new ApplicationException("arg must be exactly of type TypeReference or TypeDefinition");

            if (ReferenceEquals(a, b))
                return true;

            return a.Name == b.Name &&
                a.Namespace == b.Namespace &&
                compareTypes(a.DeclaringType, b.DeclaringType) &&
                compareScope(a.Scope, b.Scope);
        }
Esempio n. 10
0
 private void AssertTypeReferencesAreIdentical(TypeReference expected, TypeReference actual)
 {
     Assert.Equal(expected.GetType(), actual.GetType());
     this.AssertGenericParametersAreIdentical(expected, actual);
     if (expected is GenericInstanceType)
     {
         this.AssertGenericArgumentsAreIdentical((GenericInstanceType)expected, (GenericInstanceType)actual);
     }
 }
		static bool AreSame (TypeReference a, TypeReference b)
		{
			while (a is TypeSpecification || b is TypeSpecification) {
				if (a.GetType () != b.GetType ())
					return false;

				a = ((TypeSpecification) a).ElementType;
				b = ((TypeSpecification) b).ElementType;
			}

			if (a is GenericParameter || b is GenericParameter) {
				if (a.GetType() != b.GetType())
					return false;

				GenericParameter pa = (GenericParameter) a;
				GenericParameter pb = (GenericParameter) b;

				return pa.Position == pb.Position;
			}

			return a.FullName == b.FullName;
		}
		static bool AreSame (TypeReference a, TypeReference b)
		{
			if (a is TypeSpecification || b is TypeSpecification) {
				if (a.GetType () != b.GetType ())
					return false;

				return AreSame ((TypeSpecification) a, (TypeSpecification) b);
			}

			if (a is GenericParameter || b is GenericParameter) {
				if (a.GetType () != b.GetType ())
					return false;

				return AreSame ((GenericParameter) a, (GenericParameter) b);
			}

			return a.FullName == b.FullName;
		}
Esempio n. 13
0
        private void DoValidateRoot(string label, TypeReference type)
        {
            string mesg = null;

            if (type != null)
            {
                if (type is TypeSpecification)
                    mesg = string.Format("{0} is a {1} ({2})", label, type.GetType(), type.FullName);

                else
                    // Probably not neccesary to do these checks but it shouldn't hurt.
                    DoValidateRoot(label, type.FullName);
            }

            if (mesg != null)
                Contract.Assert(false, mesg);
        }
Esempio n. 14
0
        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;
        }
Esempio n. 15
0
 public static void DumpType(TypeReference type)
 {
     TraceVerbose("TYPE DUMP: {0}: {1}", type, type.GetType());
       DumpGenericParameters(type);
       DumpGenericArguments(type);
 }
Esempio n. 16
0
        public static bool compareTypes(TypeReference a, TypeReference b)
        {
            if (ReferenceEquals(a, b))
                return true;
            if (a == null || b == null)
                return false;

            var atype = a.GetType();
            var btype = b.GetType();
            if (atype != btype) {
                if ((atype == typeof(TypeReference) || atype == typeof(TypeDefinition)) &&
                    (btype == typeof(TypeReference) || btype == typeof(TypeDefinition)))
                    return compareTypeReferences(a, b);
                return false;
            }

            var type = getMemberReferenceType(a);
            switch (type) {
            case CecilType.ArrayType:
                return compareArrayTypes((ArrayType)a, (ArrayType)b);
            case CecilType.ByReferenceType:
                return compareByReferenceTypes((ByReferenceType)a, (ByReferenceType)b);
            case CecilType.FunctionPointerType:
                return compareFunctionPointerTypes((FunctionPointerType)a, (FunctionPointerType)b);
            case CecilType.GenericInstanceType:
                return compareGenericInstanceTypes((GenericInstanceType)a, (GenericInstanceType)b);
            case CecilType.GenericParameter:
                return compareGenericParameters((GenericParameter)a, (GenericParameter)b);
            case CecilType.OptionalModifierType:
                return compareOptionalModifierTypes((OptionalModifierType)a, (OptionalModifierType)b);
            case CecilType.PinnedType:
                return comparePinnedTypes((PinnedType)a, (PinnedType)b);
            case CecilType.PointerType:
                return comparePointerTypes((PointerType)a, (PointerType)b);
            case CecilType.RequiredModifierType:
                return compareRequiredModifierTypes((RequiredModifierType)a, (RequiredModifierType)b);
            case CecilType.SentinelType:
                return compareSentinelTypes((SentinelType)a, (SentinelType)b);
            case CecilType.TypeDefinition:
                return compareTypeDefinitions((TypeDefinition)a, (TypeDefinition)b);
            case CecilType.TypeReference:
                return compareTypeReferences((TypeReference)a, (TypeReference)b);
            default:
                throw new ApplicationException(string.Format("Unknown cecil type {0}", type));
            }
        }
Esempio n. 17
0
 /// <summary>
 /// This method resolves a type. This method ignores the methods and fields. You have to
 /// resolve them manually.
 /// </summary>
 /// <param name="hostModule">The module in which the changes are made.</param>
 /// <param name="type">The type to resolve.</param>
 /// <param name="AddedClasses">Newly added classes to lookup while resolving.</param>
 /// <param name="TypesMap">A map of types to lookup while resolving.</param>
 /// <returns></returns>
 protected static TypeReference Resolve(
     ModuleDefinition hostModule,
     TypeReference type,
     Dictionary<TypeReference, TypeDefinition> AddedClasses,
     Dictionary<TypeReference, TypeReference> TypesMap)
 {
     if (type is GenericInstanceType)
     {
         GenericInstanceType gType = (GenericInstanceType)type;
         GenericInstanceType nType = new GenericInstanceType(Resolve(hostModule, gType.ElementType, AddedClasses, TypesMap));
         foreach (TypeReference t in gType.GenericArguments)
         {
             nType.GenericArguments.Add(Resolve(hostModule, t, AddedClasses, TypesMap));
         }
         return nType;
     }
     if (type == null || type is GenericParameter || (type.IsArray && type.GetElementType() is GenericParameter))
         return type;
     if (TypesMap.ContainsKey(type))
         return hostModule.Import(TypesMap[type]);
     foreach (TypeReference addedType in AddedClasses.Keys)
     {
         if (addedType == type)
         {
             return hostModule.Import(AddedClasses[addedType]);
         }
     }
     if (type.Module != hostModule)
     {
         TypeDefinition t = hostModule.GetType(type.FullName);
         if (t != null)
         {
             return (TypeReference)t;
         }
         if (hostModule == null || type == null)
             return type;
         else
         {
             try
             {
                 return hostModule.Import(type);
             }
             catch (Exception e)
             {
                 System.Console.WriteLine(type.GetElementType());
                 System.Console.WriteLine(type.GetType().FullName);
                 throw e;
             }
         }
     }
     else
         return type;
 }
Esempio n. 18
0
        public static bool IsMatch(TypeReference a, TypeReference b)
        {
            if (a is TypeSpecification || b is TypeSpecification) {
            if (a.GetType() != b.GetType()) {
              return false;
            }
            return IsMatch((TypeSpecification)a, (TypeSpecification)b);
              }

              if (a is GenericParameter || b is GenericParameter) {
            if (a.GetType() != b.GetType()) {
              return false;
            }
              }

              return a.FullName == b.FullName;
        }