Exemplo n.º 1
0
            private static bool IsValueTuple(GenericInstantiationTypeReference genericInstantiationTypeReference, int minArgs)
            {
                var args = genericInstantiationTypeReference.GenericTypeArguments;

                return(args.Count >= minArgs &&
                       args.Count <= 8 &&
                       genericInstantiationTypeReference.TypeDefinition is TopLevelTypeReference topLevel &&
                       topLevel.Name == ValueTupleNamesByArity[args.Count] &&
                       topLevel.Namespace == "System");
            }
Exemplo n.º 2
0
            public ImmutableNode <string> Visit(GenericInstantiationTypeReference genericInstantiationTypeReference)
            {
                var args = genericInstantiationTypeReference.GenericTypeArguments;

                if (args.Count == 1 &&
                    genericInstantiationTypeReference.TypeDefinition is TopLevelTypeReference topLevel &&
                    topLevel.Name == "Nullable`1" &&
                    topLevel.Namespace == "System")
                {
                    return(new ImmutableNode <string>(args[0].Accept(this), "?", null));
                }
                if (IsValueTuple(genericInstantiationTypeReference, minArgs: 2) && TryUseTupleSyntax(args, out var tupleSyntax))
                {
                    return(tupleSyntax);
                }

                var builder       = (ImmutableNode <string>)null;
                var argumentsLeft = genericInstantiationTypeReference.GenericTypeArguments.Count;

                void BuildNext(string typeName)
                {
                    var(name, arity) = NameUtils.ParseGenericArity(typeName);
                    if (arity != 0)
                    {
                        if (argumentsLeft < arity)
                        {
                            throw new InvalidOperationException("Number of generic arguments provided does not match combined type arity.");
                        }

                        builder = new ImmutableNode <string>(null, ">", builder);
                        for (var i = 0; i < arity; i++)
                        {
                            argumentsLeft--;
                            builder = new ImmutableNode <string>(
                                genericInstantiationTypeReference.GenericTypeArguments[argumentsLeft].Accept(this),
                                i == 0 ? null : ", ",
                                builder);
                        }
                        builder = new ImmutableNode <string>(null, "<", builder);
                    }
                    builder = new ImmutableNode <string>(null, name, builder);
                }

                var currentType = genericInstantiationTypeReference.TypeDefinition;

                for (; currentType is NestedTypeReference nested; currentType = nested.DeclaringType)
                {
                    BuildNext(nested.Name);
                    builder = new ImmutableNode <string>(null, ".", builder);
                }

                topLevel = currentType as TopLevelTypeReference ??
                           throw new InvalidOperationException("Nested types must be declared by either a top-level type or another nested type.");

                BuildNext(topLevel.Name);

                if (argumentsLeft != 0)
                {
                    throw new InvalidOperationException("Number of generic arguments provided does not match combined type arity.");
                }
                return(AddNamespace(topLevel, builder));
            }