private static TypeSymbol GetNextDeclaredBase(NamedTypeSymbol type, ConsList <TypeSymbol> basesBeingResolved, CSharpCompilation compilation, ref PooledHashSet <NamedTypeSymbol> visited)
        {
            // We shouldn't have visited this type earlier.
            Debug.Assert(visited == null || !visited.Contains(type.OriginalDefinition));

            if (basesBeingResolved != null && basesBeingResolved.ContainsReference(type.OriginalDefinition))
            {
                return(null);
            }

            if (type.SpecialType == SpecialType.System_Object)
            {
                type.SetKnownToHaveNoDeclaredBaseCycles();
                return(null);
            }

            var nextType = type.GetDeclaredBaseType(basesBeingResolved);

            // types with no declared bases inherit object's members
            if ((object)nextType == null)
            {
                SetKnownToHaveNoDeclaredBaseCycles(ref visited);
                return(GetDefaultBaseOrNull(type, compilation));
            }

            var origType = type.OriginalDefinition;

            if (nextType.KnownToHaveNoDeclaredBaseCycles)
            {
                origType.SetKnownToHaveNoDeclaredBaseCycles();
                SetKnownToHaveNoDeclaredBaseCycles(ref visited);
            }
            else
            {
                // start cycle tracking
                visited = visited ?? PooledHashSet <NamedTypeSymbol> .GetInstance();

                visited.Add(origType);
                if (visited.Contains(nextType.OriginalDefinition))
                {
                    return(GetDefaultBaseOrNull(type, compilation));
                }
            }

            return(nextType);
        }