Example #1
0
        /// <summary>
        /// Allocates a reference to a method.
        /// </summary>
        /// <param name="host">Provides a standard abstraction over the applications that host components that provide or consume objects from the metadata model.</param>
        /// <param name="containingType">A reference to the containing type of the referenced method.</param>
        /// <param name="callingConvention">The calling convention of the referenced method.</param>
        /// <param name="returnType">The return type of the referenced method.</param>
        /// <param name="name">The name of the referenced method.</param>
        /// <param name="genericParameterCount">The number of generic parameters of the referenced method. Zero if the referenced method is not generic.</param>
        /// <param name="parameters">Information about the parameters forming part of the signature of the referenced method.</param>
        /// <param name="extraParameterTypes">Reference to the types of the the extra arguments supplied by the method call that uses this reference.</param>
        public MethodReference(IMetadataHost host, ITypeReference containingType, CallingConvention callingConvention,
                               ITypeReference returnType, IName name, ushort genericParameterCount,
                               IEnumerable <IParameterTypeInformation> parameters, params ITypeReference[] extraParameterTypes)
        {
            this.host          = host;
            _containingType    = containingType;
            _callingConvention = callingConvention;
            _type = returnType;
            _name = name;
            _genericParameterCount = genericParameterCount;
            _parameters            = parameters;
            _parameterCount        = (ushort)IteratorHelper.EnumerableCount(parameters);
            List <IParameterTypeInformation> extraParameters = new List <IParameterTypeInformation>(extraParameterTypes.Length);

            for (ushort i = 0; i < extraParameterTypes.Length; i++)
            {
                extraParameters.Add(new SimpleParameterTypeInformation(this, i, extraParameterTypes[i]));
            }
            _extraParameters = extraParameters.AsReadOnly();
        }
Example #2
0
 /// <summary>
 /// Returns true if the two signatures match according to the criteria of the CLR loader.
 /// </summary>
 public static bool SignaturesAreEqual(ISignature signature1, ISignature signature2)
 {
     if (signature1.CallingConvention != signature2.CallingConvention)
     {
         return(false);
     }
     if (signature1.ReturnValueIsByRef != signature2.ReturnValueIsByRef)
     {
         return(false);
     }
     if (signature1.ReturnValueIsModified != signature2.ReturnValueIsModified)
     {
         return(false);
     }
     if (!TypeHelper.TypesAreEquivalent(signature1.Type, signature2.Type))
     {
         return(false);
     }
     return(IteratorHelper.EnumerablesAreEqual(signature1.Parameters, signature2.Parameters, ParameterInformationComparer));
 }
Example #3
0
        /// <summary>
        /// Computes the string representing the strong name of the given assembly reference.
        /// </summary>
        public static string StrongName(IAssemblyReference assemblyReference)
        {
            Contract.Requires(assemblyReference != null);
            Contract.Ensures(Contract.Result <string>() != null);

            StringBuilder sb = StringBuilderCache.Acquire();

            sb.AppendQuoted(assemblyReference.Name.Value);
            sb.AppendFormat(CultureInfo.InvariantCulture, ", Version={0}.{1}.{2}.{3}", assemblyReference.Version.Major, assemblyReference.Version.Minor, assemblyReference.Version.Build, assemblyReference.Version.Revision);
            if (assemblyReference.Culture.Length > 0)
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, ", Culture=");
                sb.AppendQuoted(assemblyReference.Culture);
            }
            else
            {
                sb.Append(", Culture=neutral");
            }
            sb.AppendFormat(CultureInfo.InvariantCulture, ", PublicKeyToken=");
            if (IteratorHelper.EnumerableIsNotEmpty(assemblyReference.PublicKeyToken))
            {
                foreach (byte b in assemblyReference.PublicKeyToken)
                {
                    sb.Append(b.ToString("x2", CultureInfo.InvariantCulture));
                }
            }
            else
            {
                sb.Append("null");
            }
            if (assemblyReference.IsRetargetable)
            {
                sb.Append(", Retargetable=Yes");
            }
            if (assemblyReference.ContainsForeignTypes)
            {
                sb.Append(", ContentType=WindowsRuntime");
            }
            return(StringBuilderCache.GetStringAndRelease(sb));
        }
Example #4
0
        /// <summary>
        /// Returns true if the given object is an identifier that identifies the same object as this identifier.
        /// </summary>
        //^ [Confined]
        public sealed override bool Equals(object /*?*/ obj)
        {
            if (obj == (object)this)
            {
                return(true);
            }
            AssemblyIdentity /*?*/ otherAssembly = obj as AssemblyIdentity;

            if (otherAssembly == null)
            {
                return(false);
            }
            if (this.Name.UniqueKeyIgnoringCase != otherAssembly.Name.UniqueKeyIgnoringCase)
            {
                return(false);
            }
            if (this.Version != otherAssembly.Version)
            {
                return(false);
            }
            if (string.Compare(this.Culture, otherAssembly.Culture, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }
            if (IteratorHelper.EnumerableIsNotEmpty(this.PublicKeyToken))
            {
                return(IteratorHelper.EnumerablesAreEqual(this.PublicKeyToken, otherAssembly.PublicKeyToken));
            }
            else
            {
                if (this.Location.Length == 0 || otherAssembly.Location.Length == 0)
                {
                    return(true);
                }
                return(string.Compare(this.Location, otherAssembly.Location, StringComparison.OrdinalIgnoreCase) == 0);
            }
        }
Example #5
0
        /// <summary>
        /// Returns true if the given object is an identifier that identifies the same object as this identifier.
        /// </summary>
        //^ [Confined]
        public sealed override bool Equals(object /*?*/ obj)
        {
            if (obj == (object)this)
            {
                return(true);
            }
            AssemblyIdentity /*?*/ otherAssembly = obj as AssemblyIdentity;

            if (otherAssembly == null)
            {
                return(false);
            }
            if (this.Name.UniqueKeyIgnoringCase != otherAssembly.Name.UniqueKeyIgnoringCase)
            {
                return(false);
            }
            if (this.Version != otherAssembly.Version)
            {
                return(false);
            }
            if (string.Compare(this.Culture, otherAssembly.Culture, StringComparison.OrdinalIgnoreCase) != 0)
            {
                return(false);
            }
            if (IteratorHelper.EnumerableIsNotEmpty(this.PublicKeyToken))
            {
                return(IteratorHelper.EnumerablesAreEqual(this.PublicKeyToken, otherAssembly.PublicKeyToken));
            }
            else
            {
                // This can be dangerous! Returning true here means that weakly named assemblies are assumed to be the
                // same just because their name is the same. So two assemblies from different locations but the same name
                // should *NOT* be allowed.
                return(true);
            }
        }
Example #6
0
 /// <summary>
 /// Returns true if the given two assembly references are to be considered equivalent.
 /// </summary>
 public static bool AssembliesAreEquivalent(IAssemblyReference /*?*/ assembly1, IAssemblyReference /*?*/ assembly2)
 {
     if (assembly1 == null || assembly2 == null)
     {
         return(false);
     }
     if (assembly1 == assembly2)
     {
         return(true);
     }
     if (assembly1.Name.UniqueKeyIgnoringCase != assembly2.Name.UniqueKeyIgnoringCase)
     {
         return(false);
     }
     if (!assembly1.Version.Equals(assembly2.Version))
     {
         return(false);
     }
     if (!assembly1.Culture.Equals(assembly2.Culture))
     {
         return(false);
     }
     return(IteratorHelper.EnumerablesAreEqual <byte>(assembly1.PublicKeyToken, assembly2.PublicKeyToken));
 }
        /// <summary>
        /// Return zero or more locations in primary source documents that correspond to the definition of the given local.
        /// </summary>
        public IEnumerable <IPrimarySourceLocation> GetPrimarySourceLocationsForDefinitionOf(ILocalDefinition localDefinition)
        {
            PdbFunction /*?*/ pdbFunction = this.GetPdbFunctionFor(localDefinition);

            if (pdbFunction != null)
            {
                uint index = 0;
                foreach (ILocation location in localDefinition.Locations)
                {
                    IILLocation /*?*/ mbLocation = location as IILLocation;
                    if (mbLocation != null)
                    {
                        index = mbLocation.Offset;
                        break;
                    }
                }
                PdbSlot /*?*/ slot = this.GetSlotFor(pdbFunction.scopes, index);
                if (slot != null && (slot.flags & 0x4) == 0)
                {
                    return(IteratorHelper.GetSingletonEnumerable <IPrimarySourceLocation>(new LocalNameSourceLocation(slot.name)));
                }
            }
            return(Enumerable <IPrimarySourceLocation> .Empty);
        }
Example #8
0
 public IEnumerable <INamedTypeDefinition> GetAllTypes()
 {
     return(IteratorHelper.GetEmptyEnumerable <INamedTypeDefinition>());
 }
 public IEnumerable <ITypeDefinitionMember> GetMatchingMembers(Function <ITypeDefinitionMember, bool> predicate)
 {
     return(IteratorHelper.GetEmptyEnumerable <ITypeDefinitionMember>());
 }
 public IEnumerable <ITypeDefinitionMember> GetMembersNamed(IName name, bool ignoreCase)
 {
     return(IteratorHelper.GetEmptyEnumerable <ITypeDefinitionMember>());
 }
Example #11
0
 /// <summary>
 /// Return zero or more locations in primary source documents that correspond to the definition of the given local.
 /// </summary>
 public virtual IEnumerable <IPrimarySourceLocation> GetPrimarySourceLocationsForDefinitionOf(ILocalDefinition localDefinition)
 {
     return(IteratorHelper.GetEmptyEnumerable <IPrimarySourceLocation>());
 }
 public IEnumerable <ITypeDefinitionMember> GetMatchingMembersNamed(IName name, bool ignoreCase, Function <ITypeDefinitionMember, bool> predicate)
 {
     return(IteratorHelper.GetEmptyEnumerable <ITypeDefinitionMember>());
 }
Example #13
0
 /// <summary>
 /// Returns zero or more local (block) scopes, each defining an IL range in which an iterator local is defined.
 /// The scopes are returned by the MoveNext method of the object returned by the iterator method.
 /// The index of the scope corresponds to the index of the local. Specifically local scope i corresponds
 /// to the local stored in field &lt;localName&gt;x_i of the class used to store the local values in between
 /// calls to MoveNext.
 /// </summary>
 public virtual IEnumerable <ILocalScope> GetIteratorScopes(IMethodBody methodBody)
 {
     return(IteratorHelper.GetEmptyEnumerable <ILocalScope>());
 }
Example #14
0
 /// <summary>
 /// Returns zero or more local constant definitions that are local to the given scope.
 /// </summary>
 public virtual IEnumerable <ILocalDefinition> GetConstantsInScope(ILocalScope scope)
 {
     return(IteratorHelper.GetEmptyEnumerable <ILocalDefinition>());
 }
Example #15
0
 /// <summary>
 /// Creation helper from IEnumerable{T}
 /// </summary>
 public static ReadOnlyList <T> Create(IEnumerable <T> list)
 {
     return(Create(IteratorHelper.EnumerableCount <T>(list)));
 }
Example #16
0
 /// <summary>
 /// Returns a sequence of descriptors that define where try blocks and their associated handlers can be found in the instruction sequence.
 /// </summary>
 public IEnumerable <IOperationExceptionInformation> GetOperationExceptionInformation()
 {
     return(IteratorHelper.GetConversionEnumerable <ExceptionHandler, IOperationExceptionInformation>(this.handlers));
 }
Example #17
0
 public IEnumerable <string> GetStrings()
 {
     return(IteratorHelper.GetEmptyEnumerable <string>());
 }
Example #18
0
        /// <summary>
        /// Returns the number of least significant bits in the representation of field.Type that should be ignored when reading or writing the field value at MemberHelper.GetFieldOffset(field).
        /// </summary>
        /// <param name="field">The bit field whose bit offset is to returned.</param>
        public static uint GetFieldBitOffset(IFieldDefinition field)
        //^ requires field.IsBitField;
        {
            ITypeDefinition typeDefinition              = field.ContainingTypeDefinition;
            uint            result                      = 0;
            ushort          bitFieldAlignment           = 0;
            uint            bitOffset                   = 0;
            IEnumerable <ITypeDefinitionMember> members = typeDefinition.Members;

            if (typeDefinition.Layout == LayoutKind.Sequential)
            {
                List <IFieldDefinition> fields = new List <IFieldDefinition>(IteratorHelper.GetFilterEnumerable <ITypeDefinitionMember, IFieldDefinition>(members));
                fields.Sort(delegate(IFieldDefinition f1, IFieldDefinition f2) { return(f1.SequenceNumber - f2.SequenceNumber); });
                members = IteratorHelper.GetConversionEnumerable <IFieldDefinition, ITypeDefinitionMember>(fields);
            }
            foreach (ITypeDefinitionMember member in members)
            {
                IFieldDefinition /*?*/ f = member as IFieldDefinition;
                if (f == null || f.IsStatic)
                {
                    continue;
                }
                ushort fieldAlignment = (ushort)(TypeHelper.TypeAlignment(f.Type.ResolvedType) * 8);
                if (f == field)
                {
                    if (f.IsBitField)
                    {
                        if (bitOffset > 0 && bitOffset + f.BitLength > bitFieldAlignment)
                        {
                            bitOffset = 0;
                        }
                        return(bitOffset);
                    }
                    return(0);
                }
                uint fieldSize;
                if (f.IsBitField)
                {
                    bitFieldAlignment = fieldAlignment;
                    fieldSize         = f.BitLength;
                    if (bitOffset > 0 && bitOffset + fieldSize > fieldAlignment)
                    {
                        bitOffset = 0;
                    }
                    if (bitOffset == 0 || fieldSize == 0)
                    {
                        result    = ((result + fieldAlignment - 1) / fieldAlignment) * fieldAlignment;
                        bitOffset = 0;
                    }
                    bitOffset += fieldSize;
                }
                else
                {
                    if (bitFieldAlignment > fieldAlignment)
                    {
                        fieldAlignment = bitFieldAlignment;
                    }
                    bitFieldAlignment = 0; bitOffset = 0;
                    result            = ((result + fieldAlignment - 1) / fieldAlignment) * fieldAlignment;
                    fieldSize         = TypeHelper.SizeOfType(f.Type.ResolvedType) * 8;
                }
                result += fieldSize;
            }
            //^ assume false; //TODO: eventually prove this.
            return(0);
        }
Example #19
0
        /// <summary>
        /// Returns the number of bytes that separate the start of an instance of the items's declaring type from the start of the field itself.
        /// </summary>
        /// <param name="item">The item (field or nested type) of interests, which must not be static. </param>
        /// <param name="containingTypeDefinition">The type containing the item.</param>
        /// <returns></returns>
        public static uint ComputeFieldOffset(ITypeDefinitionMember item, ITypeDefinition containingTypeDefinition)
        //^ requires !field.IsStatic;
        {
            uint   result            = 0;
            ushort bitFieldAlignment = 0;
            uint   bitOffset         = 0;

            IEnumerable <ITypeDefinitionMember> members = containingTypeDefinition.Members;

            if (containingTypeDefinition.Layout == LayoutKind.Sequential)
            {
                List <IFieldDefinition> fields = new List <IFieldDefinition>(IteratorHelper.GetFilterEnumerable <ITypeDefinitionMember, IFieldDefinition>(members));
                fields.Sort(delegate(IFieldDefinition f1, IFieldDefinition f2) { return(f1.SequenceNumber - f2.SequenceNumber); });
                members = IteratorHelper.GetConversionEnumerable <IFieldDefinition, ITypeDefinitionMember>(fields);
            }

            foreach (ITypeDefinitionMember member in members)
            {
                INestedTypeDefinition fieldAsTypeDef = member as INestedTypeDefinition;
                if (fieldAsTypeDef != null && fieldAsTypeDef == item)
                {
                    ushort typeAlignment = (ushort)(TypeHelper.TypeAlignment(fieldAsTypeDef.ResolvedType) * 8);
                    return((((result + typeAlignment - 1) / typeAlignment) * typeAlignment) / 8);
                }
                else
                {
                    IFieldDefinition /*?*/ f = member as IFieldDefinition;
                    if (f == null || f.IsStatic)
                    {
                        continue;
                    }
                    if (f.Type.ResolvedType == item)
                    {
                        continue;                              // in case we are calculating the offset of an anonymous type, skip the implicit field of that type
                    }
                    ushort fieldAlignment = (ushort)(TypeHelper.TypeAlignment(f.Type.ResolvedType) * 8);
                    if (f == item)
                    {
                        if (f.IsBitField && bitOffset > 0 && bitOffset + f.BitLength <= bitFieldAlignment)
                        {
                            return((result - bitOffset) / 8);
                        }
                        if (bitFieldAlignment > fieldAlignment)
                        {
                            fieldAlignment = bitFieldAlignment;
                        }
                        return((((result + fieldAlignment - 1) / fieldAlignment) * fieldAlignment) / 8);
                    }
                    uint fieldSize;
                    if (f.IsBitField)
                    {
                        bitFieldAlignment = fieldAlignment;
                        fieldSize         = f.BitLength;
                        if (bitOffset > 0 && bitOffset + fieldSize > fieldAlignment)
                        {
                            bitOffset = 0;
                        }
                        if (bitOffset == 0 || fieldSize == 0)
                        {
                            result    = ((result + fieldAlignment - 1) / fieldAlignment) * fieldAlignment;
                            bitOffset = 0;
                        }
                        bitOffset += fieldSize;
                    }
                    else
                    {
                        if (bitFieldAlignment > fieldAlignment)
                        {
                            fieldAlignment = bitFieldAlignment;
                        }
                        bitFieldAlignment = 0; bitOffset = 0;
                        result            = ((result + fieldAlignment - 1) / fieldAlignment) * fieldAlignment;
                        fieldSize         = TypeHelper.SizeOfType(f.Type.ResolvedType) * 8;
                    }
                    result += fieldSize;
                }
            }

            return(0);
        }