Exemplo n.º 1
0
        public void GetFriendlyName_ShouldWorkWithArrays()
        {
            CSharpCompilation compilation = CreateCompilation(string.Empty);

            ITypeSymbol ar = compilation.CreateArrayTypeSymbol(compilation.GetSpecialType(SpecialType.System_Int32));

            Assert.That(ar.GetFriendlyName(), Is.EqualTo("System.Int32[]"));
        }
Exemplo n.º 2
0
 public TypedConstantTests()
 {
     _compilation = CreateCompilationWithMscorlib("class C {}");
     _namedType   = _compilation.GlobalNamespace.GetMember <NamedTypeSymbol>("C");
     _systemType  = _compilation.GetWellKnownType(WellKnownType.System_Type);
     _arrayType   = _compilation.CreateArrayTypeSymbol(_compilation.GetSpecialType(SpecialType.System_Object));
     _intType     = _compilation.GetSpecialType(SpecialType.System_Int32);
     _stringType  = _compilation.GetSpecialType(SpecialType.System_String);
     _enumString1 = _compilation.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(_compilation.GetSpecialType(SpecialType.System_String));
     _enumString2 = _compilation.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(_compilation.GetSpecialType(SpecialType.System_String));
 }
Exemplo n.º 3
0
 public TypedConstantTests()
 {
     compilation = CreateCompilationWithMscorlib("class C {}");
     namedType = compilation.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
     systemType = compilation.GetWellKnownType(WellKnownType.System_Type);
     arrayType = compilation.CreateArrayTypeSymbol(compilation.GetSpecialType(SpecialType.System_Object));
     intType = compilation.GetSpecialType(SpecialType.System_Int32);
     stringType = compilation.GetSpecialType(SpecialType.System_String);
     enumString1 = compilation.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(compilation.GetSpecialType(SpecialType.System_String));
     enumString2 = compilation.GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T).Construct(compilation.GetSpecialType(SpecialType.System_String));
 }
Exemplo n.º 4
0
        private ITypeSymbol GetTypeFromSpecifier(TypeSpecifier specifier)
        {
            if (cachedTypeSpecifierSymbols.TryGetValue(specifier, out var symbol))
            {
                return(symbol);
            }

            string lookupName = specifier.Name;

            // Find array ranks and remove them from the lookup name.
            // Example: int[][,] -> arrayRanks: { 1, 2 }, lookupName: int
            Stack <int> arrayRanks = new Stack <int>();

            while (lookupName.EndsWith("]"))
            {
                lookupName = lookupName.Remove(lookupName.Length - 1);
                int arrayRank = 1;
                while (lookupName.EndsWith(","))
                {
                    arrayRank++;
                    lookupName = lookupName.Remove(lookupName.Length - 1);
                }
                arrayRanks.Push(arrayRank);

                if (lookupName.Last() != '[')
                {
                    throw new Exception("Expected [ in lookupName");
                }

                lookupName = lookupName.Remove(lookupName.Length - 1);
            }

            if (specifier.GenericArguments.Count > 0)
            {
                lookupName += $"`{specifier.GenericArguments.Count}";
            }

            IEnumerable <INamedTypeSymbol> types = GetValidTypes(lookupName);

            ITypeSymbol foundType = null;

            foreach (INamedTypeSymbol t in types)
            {
                if (t != null)
                {
                    if (specifier.GenericArguments.Count > 0)
                    {
                        var typeArguments = specifier.GenericArguments
                                            .Select(baseType => baseType is TypeSpecifier typeSpec ?
                                                    GetTypeFromSpecifier(typeSpec) :
                                                    t.TypeArguments[specifier.GenericArguments.IndexOf(baseType)])
                                            .ToArray();
                        foundType = t.Construct(typeArguments);
                    }
                    else
                    {
                        foundType = t;
                    }

                    break;
                }
            }

            if (foundType != null)
            {
                // Make array
                //while (arrayRanks.TryPop(out int arrayRank))
                while (arrayRanks.Count > 0)
                {
                    int arrayRank = arrayRanks.Pop();
                    foundType = compilation.CreateArrayTypeSymbol(foundType, arrayRank);
                }
            }

            cachedTypeSpecifierSymbols.Add(specifier, foundType);

            return(foundType);
        }