Example #1
0
            public TypeSyntax Type0(Typ typ)
            {
                bool aliasable;

                if (!$"{Namespace}.".StartsWith($"{typ.Namespace}."))
                {
                    AddImport(typ.Namespace);
                    aliasable = false;
                }
                else
                {
                    aliasable = true;
                }
                SimpleNameSyntax result = IdentifierName(typ.Name);

                if (typ.IsDeprecated)
                {
                    result = result.WithIdentifier(result.Identifier.WithPragmaWarning(PragmaWarnings.Obsolete));
                }
                if (typ.GenericArgTyps != null)
                {
                    // Generic typ, so return a generic name by recursively calling this method on all type args.
                    result = GenericName(result.Identifier, TypeArgumentList(SeparatedList(typ.GenericArgTyps.Select(Type))));
                }
                if (aliasable)
                {
                    // Annotate this type to show it might need fully aliasing if there is a name collision.
                    result = result.WithAdditionalAnnotations(new SyntaxAnnotation(AliasableType, typ.Namespace));
                }
                return(result);
            }
Example #2
0
            private TypeSyntax Type0(Typ typ)
            {
                string namespaceAlias;

                if (!RequireFullyQualifiedTyp(typ) && $"{Namespace}.".StartsWith($"{typ.Namespace}."))
                {
                    // Within the current namespace and not required to force a fully-qualified name; no import required.
                    namespaceAlias = null;
                }
                else if (!_namespaceAliases.TryGetValue(typ.Namespace, out namespaceAlias))
                {
                    // A namespace that hasn't been seen before. Create an alias for it.
                    if (!_wellKnownNamespaceAliases.TryGetValue(typ.Namespace, out var rawAlias))
                    {
                        // If it's not a well-known namespace (e.g. "System"), then create an alias
                        // using the first character of each namespace part.
                        // TODO: Ensure single-character aliased are not generated; they cause a compilation error.
                        rawAlias = typ.Namespace
                                   .Split('.', StringSplitOptions.RemoveEmptyEntries)
                                   .Select(x => char.ToLowerInvariant(x[0]))
                                   .Aggregate("", (a, c) => a + c);
                    }
                    // Make sure all aliases are unique by adding a numeric suffix if necessary.
                    int index = 0;
                    namespaceAlias = rawAlias;
                    while (_namespaceAliasesOnly.Contains(namespaceAlias))
                    {
                        namespaceAlias = $"{rawAlias}{++index}";
                    }
                    // Record the alias for later.
                    _namespaceAliases.Add(typ.Namespace, namespaceAlias);
                    _namespaceAliasesOnly.Add(namespaceAlias);
                }
                SimpleNameSyntax result = IdentifierName(typ.Name);

                if (typ.IsDeprecated)
                {
                    result = result.WithIdentifier(result.Identifier.WithPragmaWarning(PragmaWarnings.Obsolete));
                }
                if (typ.GenericArgTyps != null)
                {
                    // Generic typ, so return a generic name by recursively calling this method on all type args.
                    result = GenericName(result.Identifier, TypeArgumentList(SeparatedList(typ.GenericArgTyps.Select(Type))));
                }
                // Return the final TypeSyntax, aliased or not as required.
                return(namespaceAlias == null ? (TypeSyntax)result : AliasQualifiedName(namespaceAlias, result));
            }
 public override TypeSyntax Type(Typ typ) => base.Type(typ) ?? Type0(typ);
Example #4
0
 public Array(Typ elementTyp) => ElementTyp = elementTyp;