Example #1
0
        private static string GetElementTypeName(Generator gen, ITypeRequestSource source, TypeReference t, TypeUsage usage)
        {
            if (t.FullName == "System.String")
            {
                switch (usage)
                {
                case TypeUsage.Raw: return("HSTRING");

                case TypeUsage.In: return("&HStringArg");

                case TypeUsage.Out: return("HString");

                case TypeUsage.GenericArg: return("HString");

                default: throw new NotSupportedException();
                }
            }
            else if (t.FullName == "System.Object")
            {
                switch (usage)
                {
                case TypeUsage.Raw: return("*mut IInspectable");

                case TypeUsage.GenericArg: return("IInspectable");

                case TypeUsage.In: return("&IInspectable");

                case TypeUsage.Out: return("Option<ComPtr<IInspectable>>");

                default: throw new InvalidOperationException();
                }
            }
            else if (t.FullName == "System.Guid")
            {
                return("Guid");
            }
            else if (t.IsPrimitive)
            {
                switch (t.FullName)
                {
                case "System.Boolean":
                    return("bool");

                case "System.SByte":
                    return("i8");

                case "System.Int16":
                    return("i16");

                case "System.Int32":
                    return("i32");

                case "System.Int64":
                    return("i64");

                case "System.Byte":
                    return("u8");

                case "System.UInt16":
                    return("u16");

                case "System.UInt32":
                    return("u32");

                case "System.UInt64":
                    return("u64");

                case "System.Single":
                    return("f32");

                case "System.Double":
                    return("f64");

                case "System.Char":
                    return("Char");

                default:
                    throw new NotImplementedException("Primitive type: " + t.FullName);
                }
            }
            else
            {
                var def = gen.GetTypeDefinition(t);
                source.AddDependency(def);

                string name = def.GetPath(source.Module);

                int i = name.IndexOf('`');
                if (i >= 0)
                {
                    name = name.Substring(0, i);
                }

                if (t.IsGenericInstance)
                {
                    var ty = (GenericInstanceType)t;
                    if (!ty.ContainsGenericParameter)
                    {
                        gen.AddGenericInstance(ty);
                    }
                    name += $"<{ String.Join(", ", ty.GenericArguments.Select(a => GetTypeName(gen, source, a, TypeUsage.GenericArg))) }>";
                }

                if (!t.IsValueType)
                {
                    if (usage == TypeUsage.In)
                    {
                        name = $"&{ name }";
                    }
                    else if (usage == TypeUsage.GenericArg)
                    {
                        // leave name unchanged
                    }
                    else if (usage == TypeUsage.Raw)
                    {
                        name = $"*mut { name }";
                    }
                    else if (usage == TypeUsage.Out)
                    {
                        name = $"Option<ComPtr<{ name }>>";
                    }
                    else if (usage == TypeUsage.OutNonNull)
                    {
                        name = $"ComPtr<{ name }>";
                    }
                }

                return(name);
            }
        }