private bool HasNameConflict(HashSet <string> namesOfTopLevelTypes, TEmbeddedType type, DiagnosticBag diagnostics)
        {
            Cci.INamespaceTypeDefinition def = type;

            if (namesOfTopLevelTypes.Contains(MetadataHelpers.BuildQualifiedName(def.NamespaceName, def.Name)))
            {
                // ERR_LocalTypeNameClash2/ERR_LocalTypeNameClash
                ReportNameCollisionWithAlreadyDeclaredType(type, diagnostics);
                return(true);
            }

            return(false);
        }
        public override ImmutableArray <Cci.INamespaceTypeDefinition> GetTypes(DiagnosticBag diagnostics, HashSet <string> namesOfTopLevelTypes)
        {
            if (_frozen.IsDefault)
            {
                var builder = ArrayBuilder <TEmbeddedType> .GetInstance();

                builder.AddRange(EmbeddedTypesMap.Values);
                builder.Sort(TypeComparer.Instance);

                if (ImmutableInterlocked.InterlockedInitialize(ref _frozen, builder.ToImmutableAndFree()))
                {
                    if (_frozen.Length > 0)
                    {
                        Cci.INamespaceTypeDefinition prev = _frozen[0];
                        bool reportedDuplicate            = HasNameConflict(namesOfTopLevelTypes, _frozen[0], diagnostics);

                        for (int i = 1; i < _frozen.Length; i++)
                        {
                            Cci.INamespaceTypeDefinition current = _frozen[i];

                            if (prev.NamespaceName == current.NamespaceName &&
                                prev.Name == current.Name)
                            {
                                if (!reportedDuplicate)
                                {
                                    Debug.Assert(_frozen[i - 1] == prev);

                                    // ERR_DuplicateLocalTypes3/ERR_InteropTypesWithSameNameAndGuid
                                    ReportNameCollisionBetweenEmbeddedTypes(_frozen[i - 1], _frozen[i], diagnostics);
                                    reportedDuplicate = true;
                                }
                            }
                            else
                            {
                                prev = current;
                                reportedDuplicate = HasNameConflict(namesOfTopLevelTypes, _frozen[i], diagnostics);
                            }
                        }

                        OnGetTypesCompleted(_frozen, diagnostics);
                    }
                }
            }

            return(StaticCast <Cci.INamespaceTypeDefinition> .From(_frozen));
        }
            public int Compare(TEmbeddedType x, TEmbeddedType y)
            {
                Cci.INamespaceTypeDefinition dx = x;
                Cci.INamespaceTypeDefinition dy = y;

                int result = string.Compare(dx.NamespaceName, dy.NamespaceName, StringComparison.Ordinal);

                if (result == 0)
                {
                    result = string.Compare(dx.Name, dy.Name, StringComparison.Ordinal);

                    if (result == 0)
                    {
                        // this is a name conflict.
                        result = x.AssemblyRefIndex - y.AssemblyRefIndex;
                    }
                }

                return(result);
            }