/// <summary>
        /// Gets a type by its metadata name to use for code analysis within a <see cref="Compilation"/>. This method
        /// attempts to find the "best" symbol to use for code analysis, which is the symbol matching the first of the
        /// following rules.
        ///
        /// <list type="number">
        ///   <item><description>
        ///     If only one type with the given name is found within the compilation and its referenced assemblies, that
        ///     type is returned regardless of accessibility.
        ///   </description></item>
        ///   <item><description>
        ///     If the current <paramref name="compilation"/> defines the symbol, that symbol is returned.
        ///   </description></item>
        ///   <item><description>
        ///     If exactly one referenced assembly defines the symbol in a manner that makes it visible to the current
        ///     <paramref name="compilation"/>, that symbol is returned.
        ///   </description></item>
        ///   <item><description>
        ///     Otherwise, this method returns <see langword="null"/>.
        ///   </description></item>
        /// </list>
        /// </summary>
        /// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
        /// <param name="fullyQualifiedMetadataName">The fully-qualified metadata type name to find.</param>
        /// <returns>The symbol to use for code analysis; otherwise, <see langword="null"/>.</returns>
        public static INamedTypeSymbol?GetBestTypeByMetadataName(this Compilation compilation, string fullyQualifiedMetadataName)
        {
            INamedTypeSymbol?type = null;

            foreach (var currentType in compilation.GetTypesByMetadataName(fullyQualifiedMetadataName))
            {
                if (ReferenceEquals(currentType.ContainingAssembly, compilation.Assembly))
                {
                    Debug.Assert(type is null);
                    return(currentType);
                }

                switch (currentType.GetResultantVisibility())
                {
                case Utilities.SymbolVisibility.Public:
                case Utilities.SymbolVisibility.Internal when currentType.ContainingAssembly.GivesAccessTo(compilation.Assembly):
                    break;

                default:
                    continue;
                }

                if (type is object)
                {
                    // Multiple visible types with the same metadata name are present
                    return(null);
                }

                type = currentType;
            }

            return(type);
        }
 /// <summary>
 /// Get subchains defining current type definition's inheritance.
 /// </summary>
 /// <returns>Subchains.</returns>
 internal override IEnumerable <InheritanceChain> GetSubChains()
 {
     if (_simulatedType == null)
     {
         return(new InheritanceChain[] { ContainingAssembly.GetChain(typeof(object)) });
     }
     else
     {
         return(GetSubChains(_simulatedType));
     }
 }
        public IteratorStateMachine(VariableSlotAllocator slotAllocatorOpt, TypeCompilationState compilationState, MethodSymbol iteratorMethod, int iteratorMethodOrdinal, bool isEnumerable, TypeSymbol elementType)
            : base(slotAllocatorOpt, compilationState, iteratorMethod, iteratorMethodOrdinal)
        {
            this.ElementType = TypeMap.SubstituteType(elementType).TypeSymbol;

            throw new NotImplementedException("TODO");

            var interfaces = ArrayBuilder <NamedTypeSymbol> .GetInstance();

            // TODO: Add proper arguments
            interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.core_Iterable_T_TIterator).Construct(ElementType));

            _interfaces = interfaces.ToImmutableAndFree();

            _constructor = new IteratorConstructor(this);
        }
Beispiel #4
0
        /// <summary>
        /// Gets the sub chains.
        /// </summary>
        /// <param name="type">The type which subchains are requested.</param>
        /// <returns>IEnumerable&lt;InheritanceChain&gt;.</returns>
        protected IEnumerable <InheritanceChain> GetSubChains(Type type)
        {
            if (ForcedSubTypes == null)
            {
                yield return(ContainingAssembly.GetChain(type.BaseType));

                foreach (var subType in type.GetInterfaces())
                {
                    yield return(ContainingAssembly.GetChain(subType));
                }
            }
            else
            {
                foreach (var forcedSubType in ForcedSubTypes)
                {
                    yield return(ContainingAssembly.GetChain(forcedSubType));
                }
            }
        }
Beispiel #5
0
            public IteratorClass(MethodSymbol method, bool isEnumerable, TypeSymbol elementType, TypeCompilationState compilationState)
                : base(method, GeneratedNames.MakeIteratorOrAsyncDisplayClassName(method.Name, compilationState.GenerateTempNumber()), TypeKind.Class)
            {
                this.ElementType = TypeMap.SubstituteType(elementType);
                var interfaces = ArrayBuilder <NamedTypeSymbol> .GetInstance();

                if (isEnumerable)
                {
                    interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(ElementType));
                    interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_IEnumerable));
                }

                interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerator_T).Construct(ElementType));
                interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_IDisposable));
                interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_IEnumerator));
                this.interfaces = interfaces.ToImmutableAndFree();

                this.constructor = new IteratorConstructor(this);
            }
Beispiel #6
0
        public IteratorStateMachine(VariableSlotAllocator slotAllocatorOpt, TypeCompilationState compilationState, MethodSymbol iteratorMethod, int iteratorMethodOrdinal, bool isEnumerable, TypeWithAnnotations elementType)
            : base(slotAllocatorOpt, compilationState, iteratorMethod, iteratorMethodOrdinal)
        {
            this.ElementType = TypeMap.SubstituteType(elementType);

            var interfaces = ArrayBuilder <NamedTypeSymbol> .GetInstance();

            if (isEnumerable)
            {
                interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(ElementType.Type));
                interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_IEnumerable));
            }

            interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerator_T).Construct(ElementType.Type));
            interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_IDisposable));
            interfaces.Add(ContainingAssembly.GetSpecialType(SpecialType.System_Collections_IEnumerator));
            _interfaces = interfaces.ToImmutableAndFree();

            _constructor = new IteratorConstructor(this);
        }
Beispiel #7
0
        internal IEnumerable <NamedTypeSymbol> GetForwardedTypes()
        {
            foreach (KeyValuePair <string, (int FirstIndex, int SecondIndex)> forwarder in Module.GetForwardedTypes())
            {
                var name = MetadataTypeName.FromFullName(forwarder.Key);

                Debug.Assert(forwarder.Value.FirstIndex >= 0, "First index should never be negative");
                AssemblySymbol firstSymbol = this.GetReferencedAssemblySymbol(forwarder.Value.FirstIndex);
                Debug.Assert((object)firstSymbol != null, "Invalid indexes (out of bound) are discarded during reading metadata in PEModule.EnsureForwardTypeToAssemblyMap()");

                if (forwarder.Value.SecondIndex >= 0)
                {
                    var secondSymbol = this.GetReferencedAssemblySymbol(forwarder.Value.SecondIndex);
                    Debug.Assert((object)secondSymbol != null, "Invalid indexes (out of bound) are discarded during reading metadata in PEModule.EnsureForwardTypeToAssemblyMap()");

                    yield return(ContainingAssembly.CreateMultipleForwardingErrorTypeSymbol(ref name, this, firstSymbol, secondSymbol));
                }
                else
                {
                    yield return(firstSymbol.LookupTopLevelMetadataType(ref name, digThroughForwardedTypes: true));
                }
            }
        }
 private NamedTypeSymbol GetDefaultBaseType()
 {
     return(ContainingAssembly.GetSpecialType(SpecialType.System_Object));
 }
 /// <summary>
 /// Gets methods defined by current type definition.
 /// </summary>
 /// <returns>Defined methods</returns>
 internal override IEnumerable <RuntimeMethodGenerator> GetMethods()
 {
     return(ContainingAssembly.GetMethodGenerators(this));
 }