Пример #1
0
        /// <summary>
        /// Get the reference of a method in the closure class as used by other methods in the same class.
        /// </summary>
        private IMethodReference GetReferenceOfMethodUsedByPeers(IMethodDefinition method)
        {
            IMethodReference methodReference = null;
            ITypeReference   typeReference   = this.ClosureDefinitionReference;
            ISpecializedNestedTypeReference specializedNestedTypeRef = typeReference as ISpecializedNestedTypeReference;
            IGenericTypeInstanceReference   genericInstanceRef       = typeReference as IGenericTypeInstanceReference;

            if (specializedNestedTypeRef != null || genericInstanceRef != null)
            {
                methodReference = new SpecializedMethodReference()
                {
                    ContainingType        = typeReference,
                    GenericParameterCount = method.GenericParameterCount,
                    InternFactory         = this.host.InternFactory,
                    UnspecializedVersion  = method,
                    Type = method.Type,
                    Name = method.Name,
                    CallingConvention     = method.CallingConvention,
                    Parameters            = method.ParameterCount == 0 ? null : new List <IParameterTypeInformation>(((IMethodReference)method).Parameters),
                    ExtraParameters       = null,
                    ReturnValueIsByRef    = method.ReturnValueIsByRef,
                    ReturnValueIsModified = method.ReturnValueIsModified,
                    Attributes            = null,
                };
            }
            else
            {
                methodReference = method;
            }
            return(methodReference);
        }
Пример #2
0
        /// <summary>
        /// See if a type reference refers to a type definition that is compiler generated.
        /// </summary>
        /// <param name="typeReference"></param>
        /// <returns></returns>
        internal static bool IsCompilerGenerated(ITypeReference /*!*/ typeReference)
        {
            if (AttributeHelper.Contains(typeReference.ResolvedType.Attributes, typeReference.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return(true);
            }
            IGenericTypeInstanceReference genericTypeInstanceReference = typeReference as IGenericTypeInstanceReference;

            if (genericTypeInstanceReference != null && IsCompilerGenerated(genericTypeInstanceReference.GenericType))
            {
                return(true);
            }
            ISpecializedNestedTypeReference specializedNestedType = typeReference as ISpecializedNestedTypeReference;

            if (specializedNestedType != null && IsCompilerGenerated(specializedNestedType.UnspecializedVersion))
            {
                return(true);
            }
            ISpecializedNestedTypeDefinition specializedNestedTypeDefinition = typeReference as ISpecializedNestedTypeDefinition;

            if (specializedNestedTypeDefinition != null && IsCompilerGenerated(specializedNestedTypeDefinition.UnspecializedVersion))
            {
                return(true);
            }
            INestedTypeReference nestedTypeReference = UnspecializedMethods.AsUnspecializedNestedTypeReference(typeReference);

            if (nestedTypeReference != null)
            {
                return(IsCompilerGenerated(nestedTypeReference.ContainingType));
            }
            return(false);
        }
 public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     if (Process(genericTypeInstanceReference))
     {
         visitor.Visit(genericTypeInstanceReference);
     }
     base.Visit(genericTypeInstanceReference);
 }
Пример #4
0
        private static void VisitTypeReference(ITypeReference typeReference, EmitContext context)
        {
            Debug.Assert(typeReference != null);

            IArrayTypeReference arrayType = typeReference as IArrayTypeReference;

            if (arrayType != null)
            {
                VisitTypeReference(arrayType.GetElementType(context), context);
                return;
            }

            IPointerTypeReference pointerType = typeReference as IPointerTypeReference;

            if (pointerType != null)
            {
                VisitTypeReference(pointerType.GetTargetType(context), context);
                return;
            }

            //IManagedPointerTypeReference managedPointerType = typeReference as IManagedPointerTypeReference;
            //if (managedPointerType != null)
            //{
            //    VisitTypeReference(managedPointerType.GetTargetType(this.context));
            //    return;
            //}

            IModifiedTypeReference modifiedType = typeReference as IModifiedTypeReference;

            if (modifiedType != null)
            {
                foreach (var custModifier in modifiedType.CustomModifiers)
                {
                    VisitTypeReference(custModifier.GetModifier(context), context);
                }
                VisitTypeReference(modifiedType.UnmodifiedType, context);
                return;
            }

            // Visit containing type
            INestedTypeReference nestedType = typeReference.AsNestedTypeReference;

            if (nestedType != null)
            {
                VisitTypeReference(nestedType.GetContainingType(context), context);
            }

            // Visit generic arguments
            IGenericTypeInstanceReference genericInstance = typeReference.AsGenericTypeInstanceReference;

            if (genericInstance != null)
            {
                foreach (var arg in genericInstance.GetGenericArguments(context))
                {
                    VisitTypeReference(arg, context);
                }
            }
        }
Пример #5
0
        protected override string GetGenericTypeInstanceName(IGenericTypeInstanceReference genericTypeInstance, NameFormattingOptions formattingOptions)
        {
            Contract.Requires(genericTypeInstance != null);
            Contract.Ensures(Contract.Result <string>() != null);

            ITypeReference genericType = genericTypeInstance.GenericType;

            if ((formattingOptions & NameFormattingOptions.ContractNullable) != 0)
            {
                if (TypeHelper.TypesAreEquivalent(genericType, genericTypeInstance.PlatformType.SystemNullable))
                {
                    foreach (ITypeReference tref in genericTypeInstance.GenericArguments)
                    {
                        return(this.GetTypeName(tref, formattingOptions) + "?");
                    }
                }
            }
            if ((formattingOptions & NameFormattingOptions.OmitTypeArguments) == 0)
            {
                // Don't include the type parameters if we are to include the type arguments
                // If formatting for a documentation id, don't use generic type name suffixes.
                StringBuilder sb = new StringBuilder(this.GetTypeName(genericType, formattingOptions & ~(NameFormattingOptions.TypeParameters | ((formattingOptions & NameFormattingOptions.FormattingForDocumentationId) != 0 ? NameFormattingOptions.UseGenericTypeNameSuffix : NameFormattingOptions.None))));
                if ((formattingOptions & NameFormattingOptions.FormattingForDocumentationId) != 0)
                {
                    sb.Append("{");
                }
                else
                {
                    sb.Append("(Of ");
                }
                bool   first = true;
                string delim = ((formattingOptions & NameFormattingOptions.OmitWhiteSpaceAfterListDelimiter) == 0) ? ", " : ",";
                foreach (ITypeReference argument in genericTypeInstance.GenericArguments)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(delim);
                    }
                    sb.Append(this.GetTypeName(argument, formattingOptions & ~(NameFormattingOptions.MemberKind | NameFormattingOptions.DocumentationIdMemberKind)));
                }
                if ((formattingOptions & NameFormattingOptions.FormattingForDocumentationId) != 0)
                {
                    sb.Append("}");
                }
                else
                {
                    sb.Append(")");
                }
                return(sb.ToString());
            }
            //If type arguments are not wanted, then type parameters are not going to be welcome either.
            return(this.GetTypeName(genericType, formattingOptions & ~NameFormattingOptions.TypeParameters));
        }
Пример #6
0
    public override void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
    {
        var typ = genericTypeInstanceReference.ResolvedType;

        if (!(typ is IGenericTypeInstance) || (((IGenericTypeInstance)typ).GenericType is Dummy))
        {
            this.EmitError(genericTypeInstanceReference, ErrorCode.GenericTypeInstanceResolution);
        }
        base.TraverseChildren(genericTypeInstanceReference);
    }
Пример #7
0
        private static void AppendAssemblyQualifierIfNecessary(StringBuilder sb, ITypeReference typeReference, out bool isAssemQualified, EmitContext context)
        {
            INestedTypeReference nestedType = typeReference.AsNestedTypeReference;

            if (nestedType != null)
            {
                AppendAssemblyQualifierIfNecessary(sb, nestedType.GetContainingType(context), out isAssemQualified, context);
                return;
            }

            IGenericTypeInstanceReference genInst = typeReference.AsGenericTypeInstanceReference;

            if (genInst != null)
            {
                AppendAssemblyQualifierIfNecessary(sb, genInst.GetGenericType(context), out isAssemQualified, context);
                return;
            }

            IArrayTypeReference arrType = typeReference as IArrayTypeReference;

            if (arrType != null)
            {
                AppendAssemblyQualifierIfNecessary(sb, arrType.GetElementType(context), out isAssemQualified, context);
                return;
            }

            IPointerTypeReference pointer = typeReference as IPointerTypeReference;

            if (pointer != null)
            {
                AppendAssemblyQualifierIfNecessary(sb, pointer.GetTargetType(context), out isAssemQualified, context);
                return;
            }

            isAssemQualified = false;
            IAssemblyReference      referencedAssembly = null;
            INamespaceTypeReference namespaceType      = typeReference.AsNamespaceTypeReference;

            if (namespaceType != null)
            {
                referencedAssembly = namespaceType.GetUnit(context) as IAssemblyReference;
            }

            if (referencedAssembly != null)
            {
                var containingAssembly = context.Module.GetContainingAssembly(context);

                if (containingAssembly == null || !ReferenceEquals(referencedAssembly, containingAssembly))
                {
                    sb.Append(", ");
                    sb.Append(MetadataWriter.StrongName(referencedAssembly));
                    isAssemQualified = true;
                }
            }
        }
        internal static void GetConsolidatedTypeArguments(this ITypeReference typeReference, ArrayBuilder <ITypeReference> consolidatedTypeArguments, EmitContext context)
        {
            INestedTypeReference nestedTypeReference = typeReference.AsNestedTypeReference;

            nestedTypeReference?.GetContainingType(context).GetConsolidatedTypeArguments(consolidatedTypeArguments, context);

            IGenericTypeInstanceReference genTypeInstance = typeReference.AsGenericTypeInstanceReference;

            if (genTypeInstance != null)
            {
                consolidatedTypeArguments.AddRange(genTypeInstance.GetGenericArguments(context));
            }
        }
Пример #9
0
        public static INamedTypeReference CanonicalizeTypeReference(ITypeReference type)
        {
            while (type != null)
            {
                IModifiedTypeReference          modifiedType = type as IModifiedTypeReference;
                IPointerTypeReference           ptrType      = type as IPointerTypeReference;
                IManagedPointerType             refType      = type as IManagedPointerType;
                IArrayTypeReference             arrType      = type as IArrayTypeReference;
                IGenericTypeInstanceReference   genType      = type as IGenericTypeInstanceReference;
                ISpecializedNestedTypeReference nestedType   = type as ISpecializedNestedTypeReference;
                // TODO: Why doesn't ISpecializedNestedTypeDefinition derive from ISpecializedNestedTypeReference?
                ISpecializedNestedTypeDefinition nestedTypeDef = type as ISpecializedNestedTypeDefinition;

                if (modifiedType != null)
                {
                    type = modifiedType.UnmodifiedType;
                }
                else if (ptrType != null)
                {
                    type = ptrType.TargetType;
                }
                else if (refType != null)
                {
                    type = refType.TargetType;
                }
                else if (arrType != null)
                {
                    type = arrType.ElementType;
                }
                else if (genType != null)
                {
                    type = genType.GenericType;
                }
                else if (nestedType != null)
                {
                    type = nestedType.UnspecializedVersion;
                }
                else if (nestedTypeDef != null)
                {
                    type = nestedTypeDef.UnspecializedVersion;
                }
                else /* ITypeDefinition */
                {
                    break;
                }
            }

            return(type as INamedTypeReference);
        }
Пример #10
0
        public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
        {
            var template = genericTypeInstanceReference.GenericType;
            var specializedNestedType = template as ISpecializedNestedTypeReference;

            if (specializedNestedType != null)
            {
                template = specializedNestedType.UnspecializedVersion;
            }
            var templateType          = this.mapper.GetType(template);
            var consolidatedArguments = new List <Type>();

            this.GetConsolidatedTypeArguments(consolidatedArguments, genericTypeInstanceReference);
            this.result = templateType.MakeGenericType(consolidatedArguments.ToArray());
        }
Пример #11
0
        private bool IncludeTypeReference(ITypeReference type)
        {
            // if a generic type and one of the generic arguments are excluded
            IGenericTypeInstanceReference genericType = type as IGenericTypeInstanceReference;

            if (genericType != null && genericType.GenericArguments.Any(genArg => _docIds.Contains(genArg.DocId())))
            {
                return(false);
            }

            // if the type itself is excluded
            string typeId = type.DocId();

            return(!_docIds.Contains(typeId));
        }
Пример #12
0
        public static ITypeReference UnWrap(this ITypeReference reference)
        {
            IPointerTypeReference pointer = reference as IPointerTypeReference;

            if (pointer != null)
            {
                return(pointer.TargetType.UnWrap());
            }

            IArrayTypeReference array = reference as IArrayTypeReference;

            if (array != null)
            {
                return(array.ElementType.UnWrap());
            }

            IModifiedTypeReference modified = reference as IModifiedTypeReference;

            if (modified != null)
            {
                return(modified.UnmodifiedType.UnWrap());
            }

            ISpecializedNestedTypeReference specialized = reference as ISpecializedNestedTypeReference;

            if (specialized != null)
            {
                return(specialized.UnspecializedVersion.UnWrap());
            }

            IGenericTypeInstanceReference instantiation = reference as IGenericTypeInstanceReference;

            if (instantiation != null)
            {
                return(instantiation.GenericType.UnWrap());
            }

            Contract.Assert(reference is INamedTypeReference ||
                            reference is INestedTypeReference ||
                            reference is INamespaceTypeReference ||
                            reference is IGenericTypeParameterReference ||
                            reference is IGenericMethodParameterReference ||
                            reference is IFunctionPointerTypeReference ||
                            reference is IManagedPointerType,
                            string.Format("Unexpected type reference that we may need to unwrap {0}", (reference != null ? reference.GetType().FullName : "null")));

            return(reference);
        }
Пример #13
0
        /// <summary>
        /// Given a type, if it is a specialized type, return its generic type. Otherwise return itself.
        /// If the type reference refers to a nested type, return a reference to the uninstantiated and/or unspecialized version
        /// of the nested type. Otherwise return the given type reference.
        /// </summary>
        internal static ITypeReference /*!*/ AsUnspecializedTypeReference(ITypeReference /*!*/ typeReference)
        {
            IGenericTypeInstanceReference genericTypeInstanceReference = typeReference as IGenericTypeInstanceReference;

            if (genericTypeInstanceReference != null)
            {
                return(AsUnspecializedTypeReference(genericTypeInstanceReference.GenericType));
            }
            var specializedNestedTypeReference = typeReference as ISpecializedNestedTypeReference;

            if (specializedNestedTypeReference != null)
            {
                return(specializedNestedTypeReference.UnspecializedVersion);
            }
            return(typeReference);
        }
        public static ITypeReference UnWrap(this ITypeReference reference)
        {
            IPointerTypeReference pointer = reference as IPointerTypeReference;

            if (pointer != null)
            {
                return(pointer.TargetType.UnWrap());
            }

            IArrayTypeReference array = reference as IArrayTypeReference;

            if (array != null)
            {
                return(array.ElementType.UnWrap());
            }

            IModifiedTypeReference modified = reference as IModifiedTypeReference;

            if (modified != null)
            {
                return(modified.UnmodifiedType.UnWrap());
            }

            ISpecializedNestedTypeReference specialized = reference as ISpecializedNestedTypeReference;

            if (specialized != null)
            {
                return(specialized.UnspecializedVersion.UnWrap());
            }

            IGenericTypeInstanceReference instantiation = reference as IGenericTypeInstanceReference;

            if (instantiation != null)
            {
                return(instantiation.GenericType.UnWrap());
            }

            Contract.Assert(reference is INamedTypeReference ||
                            reference is INestedTypeReference ||
                            reference is INamespaceTypeReference ||
                            reference is IGenericTypeParameterReference ||
                            reference is IGenericMethodParameterReference ||
                            reference is IFunctionPointerTypeReference,
                            string.Format(CultureInfo.CurrentCulture, LocalizedStrings.UnexpectedTypeReference, (reference?.GetType()?.FullName ?? "null")));

            return(reference);
        }
        internal static ITypeReference GetUninstantiatedGenericType(this ITypeReference typeReference, EmitContext context)
        {
            IGenericTypeInstanceReference genericTypeInstanceReference = typeReference.AsGenericTypeInstanceReference;

            if (genericTypeInstanceReference != null)
            {
                return(genericTypeInstanceReference.GetGenericType(context));
            }

            ISpecializedNestedTypeReference specializedNestedType = typeReference.AsSpecializedNestedTypeReference;

            if (specializedNestedType != null)
            {
                return(specializedNestedType.GetUnspecializedVersion(context));
            }

            return(typeReference);
        }
Пример #16
0
        public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
        {
            // ^ ensures this.path.Count == old(this.path.Count);
            INestedTypeReference nestedType = genericTypeInstanceReference.AsNestedTypeReference;

            if (nestedType != null)
            {
                ITypeReference containingType = nestedType.GetContainingType(Context);

                if (containingType.AsGenericTypeInstanceReference != null ||
                    containingType.AsSpecializedNestedTypeReference != null)
                {
                    this.Visit(nestedType.GetContainingType(Context));
                }
            }

            this.Visit(genericTypeInstanceReference.GenericType);
            this.Visit(genericTypeInstanceReference.GetGenericArguments(Context));
        }
Пример #17
0
        public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
        {
            // ^ ensures this.path.Count == old(this.path.Count);
            INestedTypeReference nestedType = genericTypeInstanceReference.AsNestedTypeReference;

            if (nestedType != null)
            {
                ITypeReference containingType = nestedType.GetContainingType(Context);

                if (containingType.AsGenericTypeInstanceReference != null ||
                    containingType.AsSpecializedNestedTypeReference != null)
                {
                    this.Visit(nestedType.GetContainingType(Context));
                }
            }

            this.Visit(genericTypeInstanceReference.GetGenericType(Context));
            this.Visit(genericTypeInstanceReference.GetGenericArguments(Context));
        }
Пример #18
0
        /// <summary>
        /// Caching GenericTypeInstanceRefernece objects based on interned key, avoiding GenericTypeInstance.InitializeIfNecessary expense
        /// </summary>
        public IGenericTypeInstanceReference GetOrMakeGenericTypeInstanceReference(INamedTypeReference genericTypeReference, IEnumerable <ITypeReference> genericArguments)
        {
            InternFactory factory = m_factory as InternFactory;

            if (factory != null && !factory.InternKeysAreReliablyUnique)
            {
                return(new Microsoft.Cci.Immutable.GenericTypeInstanceReference(genericTypeReference, genericArguments, this, true));
            }

            uint key = m_factory.GetGenericTypeInstanceReferenceInternedKey(genericTypeReference, genericArguments);

            IGenericTypeInstanceReference type = null;

            object value;

            if (m_objects.TryGetValue(key, out value))
            {
                type = value as IGenericTypeInstanceReference;

                if (type != null && !SequenceEquals(genericArguments, type.GenericArguments))
                {
                    // We can currently get problematic cache hits here for different objects representing the same type.
                    // e.g. SignatureGenericTypeParameter from ref signature can substitute for GenericTypeParameter
                    // from def signature. This breaks assumptions that were there prior to the sharing of generic
                    // type instances that was introduced by the caching intern factory. In that particular case, it breaks
                    // the subsequent specialization of the type parameter.
                    //
                    // We should investigate how to share more in these cases, but in the meantime, we conservatively
                    // only use an existing instantiation if the genericArguments are identical object references and
                    // otherwise force a cache miss here.
                    type = null;
                }
            }

            if (type == null)
            {
                type           = new Microsoft.Cci.Immutable.GenericTypeInstanceReference(genericTypeReference, genericArguments, this, true);
                m_objects[key] = type;
            }

            return(type);
        }
Пример #19
0
        /// <summary>
        /// Given a field definition in the closure class, get its reference as will be used by the methods in the closure class.
        /// </summary>
        internal IFieldReference GetReferenceOfFieldUsedByPeers(IFieldDefinition fieldDef)
        {
            IFieldReference fieldReference = null;
            ITypeReference  typeReference  = this.ClosureDefinitionReference;
            ISpecializedNestedTypeReference nestedTypeRef          = typeReference as ISpecializedNestedTypeReference;
            IGenericTypeInstanceReference   genericTypeInstanceRef = typeReference as IGenericTypeInstanceReference;

            if (nestedTypeRef != null || genericTypeInstanceRef != null)
            {
                fieldReference = new SpecializedFieldReference()
                {
                    ContainingType       = typeReference,
                    InternFactory        = this.host.InternFactory,
                    Name                 = fieldDef.Name,
                    UnspecializedVersion = fieldDef,
                    Type                 = fieldDef.Type
                };
            }
            else
            {
                fieldReference = fieldDef;
            }
            return(fieldReference);
        }
Пример #20
0
 public abstract void Visit(IGenericTypeInstanceReference genericTypeInstanceReference);
 /// <summary>
 /// Performs some computation with the given generic type instance reference.
 /// </summary>
 public virtual void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
 }
Пример #22
0
 /// <summary>
 /// Traverses the children of the generic type instance reference.
 /// </summary>
 public virtual void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     Contract.Requires(genericTypeInstanceReference != null);
       this.TraverseChildren((ITypeReference)genericTypeInstanceReference);
       if (this.stopTraversal) return;
       this.Traverse(genericTypeInstanceReference.GenericType);
       if (this.stopTraversal) return;
       this.Traverse(genericTypeInstanceReference.GenericArguments);
 }
Пример #23
0
 public void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     this.traverser.Traverse(genericTypeInstanceReference);
 }
Пример #24
0
 private static IEnumerable<ITypeReference> AnalyzeGenericTypeReference(IGenericTypeInstanceReference typeReference)
 {
     return GetGenericType(typeReference)
         .Union(GetGenericTypeArguments(typeReference));
 }
Пример #25
0
 /// <summary>
 /// Performs some computation with the given generic type instance reference.
 /// </summary>
 public virtual void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
 }
Пример #26
0
        public override string GetTypeName(ITypeReference type, NameFormattingOptions formattingOptions)
        {
            Contract.Requires(type != null);
            Contract.Ensures(Contract.Result <string>() != null);

            if (type is Dummy)
            {
                return("Microsoft.Cci.DummyTypeReference");
            }
            if ((formattingOptions & NameFormattingOptions.UseTypeKeywords) != 0)
            {
                switch (type.TypeCode)
                {
                case PrimitiveTypeCode.Boolean: return("Boolean");

                case PrimitiveTypeCode.Char: return("Char");

                case PrimitiveTypeCode.Float32: return("Float");

                case PrimitiveTypeCode.Float64: return("Double");

                case PrimitiveTypeCode.Int16: return("Short");

                case PrimitiveTypeCode.Int32: return("Integer");

                case PrimitiveTypeCode.Int64: return("Long");

                case PrimitiveTypeCode.Int8: return("SByte");

                case PrimitiveTypeCode.String: return("String");

                case PrimitiveTypeCode.UInt16: return("UShort");

                case PrimitiveTypeCode.UInt32: return("UInteger");

                case PrimitiveTypeCode.UInt64: return("ULong");

                case PrimitiveTypeCode.UInt8: return("Byte");

                case PrimitiveTypeCode.Void: { Contract.Assert(false); throw new InvalidOperationException(); }

                case PrimitiveTypeCode.NotPrimitive:
                    if (TypeHelper.TypesAreEquivalent(type, type.PlatformType.SystemDecimal))
                    {
                        return("Decimal");
                    }
                    if (TypeHelper.TypesAreEquivalent(type, type.PlatformType.SystemObject))
                    {
                        return("Object");
                    }
                    break;
                }
            }
            IArrayTypeReference /*?*/ arrayType = type as IArrayTypeReference;

            if (arrayType != null)
            {
                return(this.GetArrayTypeName(arrayType, formattingOptions));
            }
            IFunctionPointerTypeReference /*?*/ functionPointerType = type as IFunctionPointerTypeReference;

            if (functionPointerType != null)
            {
                return(this.GetFunctionPointerTypeName(functionPointerType, formattingOptions));
            }
            IGenericTypeParameterReference /*?*/ genericTypeParam = type as IGenericTypeParameterReference;

            if (genericTypeParam != null)
            {
                return(this.GetGenericTypeParameterName(genericTypeParam, formattingOptions));
            }
            IGenericMethodParameterReference /*?*/ genericMethodParam = type as IGenericMethodParameterReference;

            if (genericMethodParam != null)
            {
                return(this.GetGenericMethodParameterName(genericMethodParam, formattingOptions));
            }
            IGenericTypeInstanceReference /*?*/ genericInstance = type as IGenericTypeInstanceReference;

            if (genericInstance != null)
            {
                return(this.GetGenericTypeInstanceName(genericInstance, formattingOptions));
            }
            INestedTypeReference /*?*/ ntTypeDef = type as INestedTypeReference;

            if (ntTypeDef != null)
            {
                return(this.GetNestedTypeName(ntTypeDef, formattingOptions));
            }
            INamespaceTypeReference /*?*/ nsTypeDef = type as INamespaceTypeReference;

            if (nsTypeDef != null)
            {
                return(this.GetNamespaceTypeName(nsTypeDef, formattingOptions));
            }
            IPointerTypeReference /*?*/ pointerType = type as IPointerTypeReference;

            if (pointerType != null)
            {
                return(this.GetPointerTypeName(pointerType, formattingOptions));
            }
            IManagedPointerTypeReference /*?*/ managedPointerType = type as IManagedPointerTypeReference;

            if (managedPointerType != null)
            {
                return(this.GetManagedPointerTypeName(managedPointerType, formattingOptions));
            }
            IModifiedTypeReference /*?*/ modifiedType = type as IModifiedTypeReference;

            if (modifiedType != null)
            {
                return(this.GetModifiedTypeName(modifiedType, formattingOptions));
            }
            if (type.ResolvedType != type && !(type.ResolvedType is Dummy))
            {
                return(this.GetTypeName(type.ResolvedType, formattingOptions));
            }
            return("unknown type: " + type.GetType().ToString());
        }
Пример #27
0
 private static IEnumerable<ITypeReference> GetGenericTypeArguments(IGenericTypeInstanceReference typeReference)
 {
     return from a in typeReference.GenericArguments
            from t in a.GetAllRealTypeReferences()
            select t;
 }
Пример #28
0
 public virtual void onMetadataElement(IGenericTypeInstanceReference genericTypeInstanceReference) { }
Пример #29
0
        unsafe uint IMetaDataImport.GetTypeDefProps(uint td, IntPtr szTypeDef, uint cchTypeDef, out uint pchTypeDef, IntPtr pdwTypeDefFlags)
        {
            pchTypeDef = 0;
            if (td == 0)
            {
                return(0);
            }
            ITypeReference t = null;

            if ((td & 0xFF000000) == 0x1B000000)
            {
                t = this.writer.typeSpecList[(int)(td & 0xFFFFFF) - 1];
                IGenericTypeInstanceReference gt = t as IGenericTypeInstanceReference;
                if (gt != null)
                {
                    t = gt.GenericType;
                }
            }
            else
            {
                t = this.writer.typeDefList[(int)(td & 0xFFFFFF) - 1];
            }
            string tName;
            uint   parentToken = 0;

            if (this.lastTd == td)
            {
                tName       = this.lastTName;
                parentToken = this.lastParentToken;
            }
            else
            {
                tName          = TypeHelper.GetTypeName(t, NameFormattingOptions.UseGenericTypeNameSuffix | NameFormattingOptions.OmitContainingType);
                this.lastTd    = td;
                this.lastTName = tName;
                ITypeReference bc = null;
                foreach (ITypeReference baseClassRef in t.ResolvedType.BaseClasses)
                {
                    bc = baseClassRef;
                }
                if (bc != null)
                {
                    parentToken = (uint)this.writer.GetTypeToken(bc);
                }
                this.lastParentToken = parentToken;
            }
            pchTypeDef = (uint)tName.Length;
            if (pchTypeDef >= cchTypeDef)
            {
                pchTypeDef = cchTypeDef - 1;
            }
            char *pTypeDef = (char *)szTypeDef.ToPointer();

            for (int i = 0; i < pchTypeDef; i++)
            {
                *(pTypeDef + i) = tName[i];
            }
            *(pTypeDef + pchTypeDef) = (char)0;
            uint *pFlags = (uint *)pdwTypeDefFlags.ToPointer();

            *(pFlags) = PeWriter.GetTypeDefFlags(t.ResolvedType);
            return(parentToken);
        }
Пример #30
0
 public override void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference) {
   base.TraverseChildren(genericTypeInstanceReference);
 }
Пример #31
0
    private void EmitTypeLoadingCallsFor(IGenericTypeInstanceReference genericTypeInstance, string typeObjectName, SetOfUints alreadyLoadedTypes) {
      Contract.Requires(genericTypeInstance != null);
      Contract.Requires(typeObjectName != null);
      Contract.Requires(alreadyLoadedTypes != null);

      this.EmitSetTypeAndSetBaseClass(genericTypeInstance.ResolvedType, typeObjectName, alreadyLoadedTypes);
      this.sourceEmitter.EmitString("AllocateForGenericArguments(");
      this.sourceEmitter.EmitString(typeObjectName);
      this.sourceEmitter.EmitString(", " + genericTypeInstance.GenericType.GenericParameterCount + ");");
      this.sourceEmitter.EmitNewLine();
      int count = 0;
      foreach (var genericArgument in genericTypeInstance.GenericArguments) {
        Contract.Assume(genericArgument != null);
        this.sourceEmitter.EmitString("SetGenericArgument(");
        this.sourceEmitter.EmitString(typeObjectName);
        this.sourceEmitter.EmitString(", ");
        this.sourceEmitter.EmitString(this.GetMangledTypeName(genericArgument)+"_typeObject");
        this.sourceEmitter.EmitString(", " + count + ");");
        this.sourceEmitter.EmitNewLine();
        count++;
      }
    }
 /// <summary>
 /// Rewrites the given generic type instance reference.
 /// </summary>
 public virtual ITypeReference Rewrite(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     return genericTypeInstanceReference;
 }
 public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     if(Process(genericTypeInstanceReference)){visitor.Visit(genericTypeInstanceReference);}
     base.Visit(genericTypeInstanceReference);
 }
Пример #34
0
 public virtual void onMetadataElement(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
 }
Пример #35
0
 public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference) {
   var template = genericTypeInstanceReference.GenericType;
   var specializedNestedType = template as ISpecializedNestedTypeReference;
   if (specializedNestedType != null) template = specializedNestedType.UnspecializedVersion;
   var templateType = this.mapper.GetType(template);
   var consolidatedArguments = new List<Type>();
   this.GetConsolidatedTypeArguments(consolidatedArguments, genericTypeInstanceReference);
   this.result = templateType.MakeGenericType(consolidatedArguments.ToArray());
 }
Пример #36
0
        public override void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
{ MethodEnter(genericTypeInstanceReference);
            base.TraverseChildren(genericTypeInstanceReference);
     MethodExit();   }
Пример #37
0
 private static IEnumerable<ITypeReference> GetGenericType(IGenericTypeInstanceReference typeReference)
 {
     yield return typeReference.GenericType;
 }
Пример #38
0
 public void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     throw new NotImplementedException();
 }
Пример #39
0
 public override void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     base.TraverseChildren(genericTypeInstanceReference);
 }
Пример #40
0
 /// <summary>
 /// Performs some computation with the given generic type instance reference.
 /// </summary>
 public void Visit(IGenericTypeInstanceReference genericTypeInstanceReference) {
   var mfmv = this.validator.currentModule.MetadataFormatMajorVersion;
   if (mfmv < 2)
     this.ReportError(MetadataError.InvalidMetadataFormatVersionForGenerics, genericTypeInstanceReference, mfmv.ToString());
   this.Visit((ITypeReference)genericTypeInstanceReference);
 }
Пример #41
0
 /// <summary>
 /// Traverses the generic type instance reference.
 /// </summary>
 public void Traverse(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     Contract.Requires(genericTypeInstanceReference != null);
       if (!this.objectsThatHaveAlreadyBeenTraversed.Add(genericTypeInstanceReference)) return;
       if (this.preorderVisitor != null) this.preorderVisitor.Visit(genericTypeInstanceReference);
       if (this.stopTraversal) return;
       this.TraverseChildren(genericTypeInstanceReference);
       if (this.stopTraversal) return;
       if (this.postorderVisitor != null) this.postorderVisitor.Visit(genericTypeInstanceReference);
 }
Пример #42
0
 public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     allElements.Add(new InvokInfo(Traverser, "IGenericTypeInstanceReference", genericTypeInstanceReference));
 }
Пример #43
0
 //^ ensures this.path.Count == old(this.path.Count);
 /// <summary>
 /// Performs some computation with the given generic type instance reference.
 /// </summary>
 /// <param name="genericTypeInstanceReference"></param>
 public virtual void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     if (this.stopTraversal) return;
       //^ int oldCount = this.path.Count;
       this.path.Push(genericTypeInstanceReference);
       this.Visit(genericTypeInstanceReference.GenericType);
       this.Visit(genericTypeInstanceReference.GenericArguments);
       //^ assume this.path.Count == oldCount; //True because all of the virtual methods of this class promise not decrease this.path.Count.
       this.path.Pop();
 }
Пример #44
0
 public override void Visit(IGenericTypeInstanceReference genericTypeInstanceReference) {
   genericTypeInstanceReference.GenericType.ResolvedType.Dispatch(this);
   foreach (var genArg in genericTypeInstanceReference.GenericArguments) {
     Contract.Assume(genArg != null);
     genArg.ResolvedType.Dispatch(this);
   }
 }
Пример #45
0
 /// <summary>
 /// Performs some computation with the given generic type instance reference.
 /// </summary>
 public void Visit(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     this.Visit((ITypeReference)genericTypeInstanceReference);
 }
Пример #46
0
 public abstract void Visit(IGenericTypeInstanceReference genericTypeInstanceReference);
Пример #47
0
        /// <summary>
        /// Use this routine, rather than ITypeReference.Dispatch, to call the appropriate derived overload of an ITypeReference.
        /// The former routine will call Visit(INamespaceTypeDefinition) rather than Visit(INamespaceTypeReference), etc.,
        /// in the case where a definition is used as a reference to itself.
        /// </summary>
        /// <param name="typeReference">A reference to a type definition. Note that a type definition can serve as a reference to itself.</param>
        protected void DispatchAsReference(ITypeReference typeReference)
        {
            INamespaceTypeReference namespaceTypeReference = typeReference.AsNamespaceTypeReference;

            if (namespaceTypeReference != null)
            {
                this.Visit(namespaceTypeReference);
                return;
            }

            IGenericTypeInstanceReference genericTypeInstanceReference = typeReference.AsGenericTypeInstanceReference;

            if (genericTypeInstanceReference != null)
            {
                this.Visit(genericTypeInstanceReference);
                return;
            }

            INestedTypeReference nestedTypeReference = typeReference.AsNestedTypeReference;

            if (nestedTypeReference != null)
            {
                this.Visit(nestedTypeReference);
                return;
            }

            IArrayTypeReference arrayTypeReference = typeReference as IArrayTypeReference;

            if (arrayTypeReference != null)
            {
                this.Visit(arrayTypeReference);
                return;
            }

            IGenericTypeParameterReference genericTypeParameterReference = typeReference.AsGenericTypeParameterReference;

            if (genericTypeParameterReference != null)
            {
                this.Visit(genericTypeParameterReference);
                return;
            }

            IGenericMethodParameterReference genericMethodParameterReference = typeReference.AsGenericMethodParameterReference;

            if (genericMethodParameterReference != null)
            {
                this.Visit(genericMethodParameterReference);
                return;
            }

            IPointerTypeReference pointerTypeReference = typeReference as IPointerTypeReference;

            if (pointerTypeReference != null)
            {
                this.Visit(pointerTypeReference);
                return;
            }

            IModifiedTypeReference modifiedTypeReference = typeReference as IModifiedTypeReference;

            if (modifiedTypeReference != null)
            {
                this.Visit(modifiedTypeReference);
                return;
            }
        }
Пример #48
0
        internal static string TypeDefinition(IUnit currentUnit, ITypeDefinition typeDefinition)
        {
            if (typeDefinition == Dummy.Type)
            {
                return("###DummyType###");
            }
            PrimitiveTypeCode ptc = typeDefinition.TypeCode;

            switch (ptc)
            {
            case PrimitiveTypeCode.Boolean:
                return("bool");

            case PrimitiveTypeCode.Char:
                return("char");

            case PrimitiveTypeCode.Int8:
                return("int8");

            case PrimitiveTypeCode.Float32:
                return("float32");

            case PrimitiveTypeCode.Float64:
                return("float64");

            case PrimitiveTypeCode.Int16:
                return("int16");

            case PrimitiveTypeCode.Int32:
                return("int32");

            case PrimitiveTypeCode.Int64:
                return("int64");

            case PrimitiveTypeCode.IntPtr:
                return("native int");

            case PrimitiveTypeCode.UInt8:
                return("unsigned int8");

            case PrimitiveTypeCode.UInt16:
                return("unsigned int16");

            case PrimitiveTypeCode.UInt32:
                return("unsigned int32");

            case PrimitiveTypeCode.UInt64:
                return("unsigned int64");

            case PrimitiveTypeCode.UIntPtr:
                return("native unsigned int");

            case PrimitiveTypeCode.Void:
                return("void");
            }
            INamespaceTypeDefinition namespaceType = typeDefinition as INamespaceTypeDefinition;

            if (namespaceType != null)
            {
                bool          wasRoot;
                StringBuilder sb = new StringBuilder(Helper.ModuleQualifiedUnitNamespace(currentUnit, (IUnitNamespace)namespaceType.ContainingNamespace, out wasRoot));
                if (!wasRoot)
                {
                    sb.Append(".");
                }
                sb.Append(namespaceType.Name.Value);
                if (namespaceType.GenericParameterCount != 0)
                {
                    sb.Append("`");
                    sb.Append(namespaceType.GenericParameterCount);
                }
                return(sb.ToString());
            }
            INestedTypeDefinition nestedTypeDefinition = typeDefinition as INestedTypeDefinition;

            if (nestedTypeDefinition != null)
            {
                StringBuilder sb = new StringBuilder(Helper.TypeDefinition(currentUnit, nestedTypeDefinition.ContainingTypeDefinition));
                sb.Append("/");
                sb.Append(nestedTypeDefinition.Name.Value);
                if (nestedTypeDefinition.GenericParameterCount != 0)
                {
                    sb.Append("`");
                    sb.Append(nestedTypeDefinition.GenericParameterCount);
                }
                return(sb.ToString());
            }
            IGenericTypeInstanceReference genericTypeInstance = typeDefinition as IGenericTypeInstanceReference;

            if (genericTypeInstance != null)
            {
                StringBuilder sb = new StringBuilder(Helper.TypeDefinition(currentUnit, genericTypeInstance.GenericType.ResolvedType));
                sb.Append("<");
                bool isNotFirst = false;
                foreach (ITypeReference typeReference in genericTypeInstance.GenericArguments)
                {
                    if (isNotFirst)
                    {
                        sb.Append(",");
                    }
                    isNotFirst = true;
                    sb.Append(Helper.TypeDefinition(currentUnit, typeReference.ResolvedType));
                }
                sb.Append(">");
                return(sb.ToString());
            }
            IPointerTypeReference pointerType = typeDefinition as IPointerTypeReference;

            if (pointerType != null)
            {
                StringBuilder sb = new StringBuilder(Helper.TypeDefinition(currentUnit, pointerType.TargetType.ResolvedType));
                sb.Append("*");
                return(sb.ToString());
            }
            IArrayTypeReference arrayType = typeDefinition as IArrayTypeReference;

            if (arrayType != null)
            {
                StringBuilder sb = new StringBuilder(Helper.TypeDefinition(currentUnit, arrayType.ElementType.ResolvedType));
                sb.Append("[");
                if (!arrayType.IsVector)
                {
                    if (arrayType.Rank == 1)
                    {
                        sb.Append("*");
                    }
                    else
                    {
                        for (int i = 1; i < arrayType.Rank; ++i)
                        {
                            sb.Append(",");
                        }
                    }
                }
                sb.Append("]");
                return(sb.ToString());
            }
            IGenericTypeParameter genericTypeParameter = typeDefinition as IGenericTypeParameter;

            if (genericTypeParameter != null)
            {
                return("!" + genericTypeParameter.Index);
            }
            IGenericMethodParameter genericMethodParameter = typeDefinition as IGenericMethodParameter;

            if (genericMethodParameter != null)
            {
                return("!!" + genericMethodParameter.Index);
            }
            return("!?!ErrorType!?!");
        }
Пример #49
0
 public override void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     MethodEnter(genericTypeInstanceReference);
     base.TraverseChildren(genericTypeInstanceReference);
     MethodExit();
 }
Пример #50
0
 public static bool IsNullableValueType(this IGenericTypeInstanceReference genericType)
 {
     return(genericType.GenericType.FullName().StartsWith("System.Nullable"));
 }
Пример #51
0
        private static void WriteType(ITypeReference type, TextWriter writer, bool omitOutermostTypeFormals)
        {
            Contract.Requires(type != null);
            Contract.Requires(writer != null);

            IArrayType array = type as IArrayType;

            if (array != null)
            {
                Contract.Assume(array.ElementType != null, "lack of CCI2 contracts");

                WriteType(array.ElementType, writer);
                writer.Write("[");
                if (array.Rank > 1)
                {
                    for (int i = 0; i < array.Rank; i++)
                    {
                        if (i > 0)
                        {
                            writer.Write(",");
                        }
                        writer.Write("0:");
                    }
                }
                writer.Write("]");
                return;
            }

            IManagedPointerTypeReference reference = type as IManagedPointerTypeReference;;

            if (reference != null)
            {
                Contract.Assume(reference.TargetType != null, "lack of CCI2 contracts");

                var referencedType = reference.TargetType;
                WriteType(referencedType, writer);
                writer.Write("@");
                return;
            }

            IPointerTypeReference pointer = type as IPointerTypeReference;

            if (pointer != null)
            {
                Contract.Assume(pointer.TargetType != null, "lack of CCI2 contracts");

                WriteType(pointer.TargetType, writer);
                writer.Write("*");
                return;
            }

            IModifiedTypeReference modref = type as IModifiedTypeReference;

            if (modref != null)
            {
                Contract.Assume(modref.UnmodifiedType != null, "lack of CCI2 contracts");
                Contract.Assume(modref.CustomModifiers != null, "lack of CCI2 contracts");

                WriteType(modref.UnmodifiedType, writer);
                foreach (var modifier in modref.CustomModifiers)
                {
                    Contract.Assume(modifier != null, "lack of collection contracts and CCI2 contracts");
                    Contract.Assume(modifier.Modifier != null, "lack of CCI2 contracts");

                    if (modifier.IsOptional)
                    {
                        writer.Write("!");
                    }
                    else
                    {
                        writer.Write("|");
                    }
                    WriteType(modifier.Modifier, writer);
                }
                return;
            }

            IGenericTypeParameterReference gtp = type as IGenericTypeParameterReference;

            if (gtp != null)
            {
                writer.Write("`");
                writer.Write(gtp.Index);
                return;
            }
            IGenericMethodParameterReference gmp = type as IGenericMethodParameterReference;

            if (gmp != null)
            {
                writer.Write("``");
                writer.Write(gmp.Index);
                return;
            }

            IGenericTypeInstanceReference instance = type as IGenericTypeInstanceReference;

            if (instance != null)
            {
                Contract.Assume(instance.GenericType != null, "lack of CCI2 contracts");
                Contract.Assume(instance.GenericArguments != null, "lack of CCI2 contracts");

                WriteType(instance.GenericType, writer, true);
                writer.Write("{");
                var first = true;
                foreach (var arg in instance.GenericArguments)
                {
                    Contract.Assume(arg != null, "lack of collection and CCI2 contracts");

                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        writer.Write(",");
                    }
                    WriteType(arg, writer);
                }
                writer.Write("}");
                return;
            }

            // namespace or nested
            INamedTypeReference  named  = (INamedTypeReference)type;
            INestedTypeReference nested = type as INestedTypeReference;

            if (nested != null)
            {
                Contract.Assume(nested.ContainingType != null, "lack of CCI2 contracts");

                // names of nested types begin with outer type name
                WriteType(nested.ContainingType, writer);
                writer.Write(".");
                // continue to write type sig
            }

            INamespaceTypeReference nt = type as INamespaceTypeReference;

            if (nt != null)
            {
                Contract.Assume(nt.ContainingUnitNamespace != null, "lack of CCI2 contracts");

                WriteNamespaceAndSeparator(nt.ContainingUnitNamespace, writer);
                // continue to write type sig
            }
            // name
            writer.Write(named.Name.Value);
            // generic parameters
            if (omitOutermostTypeFormals)
            {
                return;
            }

            if (named.GenericParameterCount > 0)
            {
                writer.Write("`{0}", named.GenericParameterCount);
            }
        }
Пример #52
0
 /// <summary>
 /// Traverses the children of the generic type instance reference.
 /// </summary>
 public virtual void TraverseChildren(IGenericTypeInstanceReference genericTypeInstanceReference)
 {
     this.TraverseChildren((ITypeReference)genericTypeInstanceReference);
       if (this.stopTraversal) return;
       this.Traverse(genericTypeInstanceReference.GenericType);
       if (this.stopTraversal) return;
       this.Traverse(genericTypeInstanceReference.GenericArguments);
 }