Example #1
0
        public static TypeReference MapDatatype(TypeReference t)
        {
            var m = t.MemberOf;
            var g = t.Container;

            t           = t.Clone() as TypeReference;
            t.MemberOf  = m;
            t.Container = g;
            TypeReference retVal = t.Clone() as TypeReference;

            // Create a type reference
            var type = s_heuristicData.Datatypes.Find(o => o.MifDatatype == t.Name + (t.Flavor != null ? "." + t.Flavor : ""));

            if (type == null && !String.IsNullOrEmpty(t.Flavor))
            {
                type = s_heuristicData.Datatypes.Find(o => o.MifDatatype == t.Name);
            }

            if (t is TypeParameter)
            {
                return(t);
            }
            else if (t.Name == null)
            {
                retVal.Name = "java.lang.Object";
            }
            else if (type != null)
            {
                retVal.Name = type.JavaType;
                //if (!String.IsNullOrEmpty(type.TemplateParameter))
                //    retVal.Name = String.Format("{0}.datatypes.generic.{1}", s_apiNs, retVal.Name);
                //else
                //    retVal.Name = String.Format("{0}.datatypes.{1}", s_apiNs, retVal.Name);

                retVal.MemberOf        = t.MemberOf;
                retVal.GenericSupplier = t.GenericSupplier;

                if (t.Name != retVal.Name)
                {
                    retVal.Flavor = null; // Clear flavors if pointing to a new type
                }
                // Default binding information
                var tParm = type.DefaultBind;
                if ((t.GenericSupplier == null || t.GenericSupplier.Count == 0) && !String.IsNullOrEmpty(tParm))
                {
                    retVal.AddGenericSupplier("def", MapDatatype(new TypeReference()
                    {
                        Name = tParm
                    }));
                }
            }
            // Default member?
            return(retVal);
        }
Example #2
0
        /// <summary>
        /// Resolves the type reference in the specified module.
        /// </summary>
        /// <param name="scope">The module where the type reference is resolved.</param>
        /// <param name="typeRef">The type reference to resolve.</param>
        /// <returns>The resolved type definition.</returns>
        public static TypeDefinition TryResolve(this ModuleDefinition scope, TypeReference typeRef)
        {
            var matchingTypeRef = typeRef.Clone();

            matchingTypeRef.Scope = scope;
            return(scope.MetadataResolver.TryResolve(matchingTypeRef));
        }
        static TypeReference Resolve(TypeReference typeRef, Collection <TypeReference> genTypeContext)
        {
            if (!(typeRef is TypeSpecification) && !(typeRef is GenericParameter))
            {
                return(typeRef);
            }

            TypeSpecification ret = typeRef.Clone() as TypeSpecification;

            switch (typeRef.MetadataType)
            {
            case MetadataType.RequiredModifier:
                (ret as RequiredModifierType).ModifierType = Resolve((typeRef as RequiredModifierType).ModifierType, genTypeContext);
                ret.ElementType = Resolve(ret.ElementType, genTypeContext);
                break;

            case MetadataType.OptionalModifier:
                (ret as OptionalModifierType).ModifierType = Resolve((typeRef as OptionalModifierType).ModifierType, genTypeContext);
                ret.ElementType = Resolve(ret.ElementType, genTypeContext);
                break;

            case MetadataType.Array:
            case MetadataType.ByReference:
            case MetadataType.Pinned:
            case MetadataType.Pointer:
                ret.ElementType = Resolve(ret.ElementType, genTypeContext);
                break;

            case MetadataType.GenericInstance:
                GenericInstanceType genInst = ret as GenericInstanceType;
                genInst.GenericArguments.Clear();
                foreach (var i in (typeRef as GenericInstanceType).GenericArguments)
                {
                    genInst.GenericArguments.Add(Resolve(i, genTypeContext));
                }
                ret.ElementType = Resolve(ret.ElementType, genTypeContext);
                break;

            case MetadataType.MVar:
                return(typeRef);

            case MetadataType.Var:
                if (genTypeContext == null)
                {
                    throw new InvalidOperationException();
                }
                return(genTypeContext[(typeRef as GenericParameter).Position]);

            case MetadataType.FunctionPointer:      //not support
                throw new NotSupportedException();
            }
            return(ret);
        }
Example #4
0
            public ConstantTypeRefResolver(TypeReference typeRef)
            {
                if (typeRef.MetadataType == MetadataType.Class && !(typeRef is TypeDefinition))
                {
                    // TypeRefs from different assemblies get imported as MetadataType.Class
                    // since this information is not stored in the assembly metadata.
                    var typeDef = typeRef.ResolveRequiredType();
                    typeRef             = typeRef.Clone();
                    typeRef.IsValueType = typeDef.IsValueType;
                }

                _typeRef = typeRef;
            }
        TypeReference GetTypeReferenceFromExpression(Expression expr)
        {
            if (expr is TypeReferenceExpression)
            {
                return((expr as TypeReferenceExpression).TypeReference);
            }

            IdentifierExpression ident = expr as IdentifierExpression;

            if (ident != null)
            {
                return(new TypeReference(ident.Identifier, ident.TypeArguments));
            }

            MemberReferenceExpression member = expr as MemberReferenceExpression;

            if (member != null)
            {
                TypeReference targetType = GetTypeReferenceFromExpression(member.TargetObject);
                if (targetType != null)
                {
                    if (targetType.GenericTypes.Count == 0 && targetType.IsArrayType == false)
                    {
                        TypeReference tr = targetType.Clone();
                        tr.Type = tr.Type + "." + member.MemberName;
                        tr.GenericTypes.AddRange(member.TypeArguments);
                        return(tr);
                    }
                    else
                    {
                        return(new InnerClassTypeReference(targetType, member.MemberName, member.TypeArguments));
                    }
                }
            }
            return(null);
        }
Example #6
0
        private TypeReferenceExpression GetTypeReferenceExpression(Expression expr, List <TypeReference> genericTypes)
        {
            TypeReferenceExpression tre = expr as TypeReferenceExpression;

            if (tre != null)
            {
                return(new TypeReferenceExpression(new TypeReference(tre.TypeReference.Type, tre.TypeReference.PointerNestingLevel, tre.TypeReference.RankSpecifier, genericTypes)));
            }
            StringBuilder b = new StringBuilder();

            if (!WriteFullTypeName(b, expr))
            {
                // there is some TypeReferenceExpression hidden in the expression
                while (expr is FieldReferenceExpression)
                {
                    expr = ((FieldReferenceExpression)expr).TargetObject;
                }
                tre = expr as TypeReferenceExpression;
                if (tre != null)
                {
                    TypeReference typeRef = tre.TypeReference;
                    if (typeRef.GenericTypes.Count == 0)
                    {
                        typeRef       = typeRef.Clone();
                        typeRef.Type += "." + b.ToString();
                        typeRef.GenericTypes.AddRange(genericTypes);
                    }
                    else
                    {
                        typeRef = new InnerClassTypeReference(typeRef, b.ToString(), genericTypes);
                    }
                    return(new TypeReferenceExpression(typeRef));
                }
            }
            return(new TypeReferenceExpression(new TypeReference(b.ToString(), 0, null, genericTypes)));
        }
    public virtual Differences VisitTypeReference(TypeReference reference1, TypeReference reference2){
      Differences differences = new Differences(reference1, reference2);
      if (reference1 == null || reference2 == null){
        if (reference1 != reference2) differences.NumberOfDifferences++; else differences.NumberOfSimilarities++;
        return differences;
      }
      UnaryExpression changes = (UnaryExpression)reference2.Clone();
      UnaryExpression deletions = (UnaryExpression)reference2.Clone();
      UnaryExpression insertions = (UnaryExpression)reference2.Clone();

      Differences diff = this.VisitTypeNode(reference1.Type, reference2.Type);
      if (diff == null){Debug.Assert(false); return differences;}
      changes.Type = diff.Changes as TypeNode;
      deletions.Type = diff.Deletions as TypeNode;
      insertions.Type = diff.Insertions as TypeNode;
      //Debug.Assert(diff.Changes == changes.Type && diff.Deletions == deletions.Type && diff.Insertions == insertions.Type);
      differences.NumberOfDifferences += diff.NumberOfDifferences;
      differences.NumberOfSimilarities += diff.NumberOfSimilarities;

      if (differences.NumberOfDifferences == 0){
        differences.Changes = null;
        differences.Deletions = null;
        differences.Insertions = null;
      }else{
        differences.Changes = changes;
        differences.Deletions = deletions;
        differences.Insertions = insertions;
      }
      return differences;
    }
Example #8
0
        public static TypeReference MapDatatype(TypeReference t)
        {
            var           m      = t.MemberOf;
            var           g      = t.Container;
            TypeReference retVal = t.Clone() as TypeReference;

            retVal.MemberOf  = m;
            retVal.Container = g;

            if (t is TypeParameter)
            {
                return(t);
            }
            else if (t.Name == null)
            {
                retVal.Name = "System.Object";
            }
            else if (flavMaps.ContainsKey(t.Name + "." + t.Flavor))
            {
                // Create a type reference
                Type type = flavMaps[t.Name + "." + t.Flavor];
                retVal.Name = type.Name;
                if (type.IsInterface)
                {
                    retVal.Name = type.FullName;
                }

                if (retVal.Name.Contains("`"))
                {
                    retVal.Name = retVal.Name.Substring(0, retVal.Name.IndexOf("`"));
                }
                retVal.MemberOf        = t.MemberOf;
                retVal.GenericSupplier = t.GenericSupplier;

                if (t.Name != retVal.Name)
                {
                    retVal.Flavor = null; // Clear flavors if pointing to a new type
                }
            }
            else if (typeMaps.ContainsKey(String.Format("{0}#{1}", t.Name, t.GenericSupplier != null && t.GenericSupplier.Count > 0 ? t.GenericSupplier[0].Name : null))) // Maps types
            {
                Type type = typeMaps[String.Format("{0}#{1}", t.Name, t.GenericSupplier != null && t.GenericSupplier.Count > 0 ? t.GenericSupplier[0].Name : null)];
                retVal.Name = type.Name;
                if (type.IsInterface)
                {
                    retVal.Name = type.FullName;
                }
                if (retVal.Name.Contains("`"))
                {
                    retVal.Name = retVal.Name.Substring(0, retVal.Name.IndexOf("`"));
                }
                retVal.MemberOf = t.MemberOf;

                if (type.IsGenericTypeDefinition)
                {
                    retVal.GenericSupplier = t.GenericSupplier;
                }
                else
                {
                    retVal.GenericSupplier = null;
                }
            }
            else if (typeMaps.ContainsKey(String.Format("{0}#", t.Name))) // Maps types
            {
                Type type = typeMaps[String.Format("{0}#", t.Name)];
                retVal.Name = type.Name;
                if (type.IsInterface)
                {
                    retVal.Name = type.FullName;
                }
                if (retVal.Name.Contains("`"))
                {
                    retVal.Name = retVal.Name.Substring(0, retVal.Name.IndexOf("`"));
                }
                retVal.MemberOf        = t.MemberOf;
                retVal.GenericSupplier = t.GenericSupplier;
            }

            // Type parm info
            var tParm = defaultTypeParms.Find(o => o.StructureAttribute.Name.Equals(t.Name));

            if (tParm != null && (t.GenericSupplier == null || t.GenericSupplier.Count == 0) && tParm.DefaultTypeParameter != null)
            {
                // Create the type parm
                var staParm = tParm.DefaultTypeParameter.GetCustomAttributes(typeof(StructureAttribute), false);
                for (int i = 0; i < tParm.Type.GetGenericArguments().Length; i++)
                {
                    if (staParm.Length > 0)
                    {
                        retVal.AddGenericSupplier(i.ToString(), MapDatatype(new TypeReference()
                        {
                            Name = (staParm[0] as StructureAttribute).Name
                        }));
                    }
                    else
                    {
                        retVal.AddGenericSupplier(i.ToString(), new TypeReference()
                        {
                            Name = tParm.DefaultTypeParameter.FullName
                        });
                    }
                }
            }

            // Default member?
            return(retVal);
        }