Example #1
0
        void WriteEnumCore(FileWriter writer, PartialEnumFileInfo info, EnumType enumType)
        {
            // Don't add the Code comments since they're generated (less useful comments) and will generate bigger ts/js files
            bool writeComments = enumType.TypeId != TypeIds.Code;

            docWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName);
            var enumTypeName = enumType.Name(idConverter);

            if (enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeWasmBindgen);
            }
            foreach (var attr in info.Attributes)
            {
                writer.WriteLine(attr);
            }
            if (!writeComments || (enumType.IsPublic && enumType.IsMissingDocs))
            {
                writer.WriteLine(RustConstants.AttributeAllowMissingDocs);
            }
            if (!enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            }
            var pub = enumType.IsPublic ? "pub " : "pub(crate) ";

            writer.WriteLine($"{pub}enum {enumTypeName} {{");
            using (writer.Indent()) {
                uint expectedValue = 0;
                foreach (var value in enumType.Values)
                {
                    // Identical enum values aren't allowed so just remove it
                    if (value.DeprecatedInfo.IsDeprecated)
                    {
                        continue;
                    }
                    if (writeComments)
                    {
                        docWriter.WriteSummary(writer, value.Documentation, enumType.RawName);
                    }
                    deprecatedWriter.WriteDeprecated(writer, value);
                    if (enumType.IsFlags)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {NumberFormatter.FormatHexUInt32WithSep(value.Value)},");
                    }
                    else if (expectedValue != value.Value || enumType.IsPublic)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {value.Value},");
                    }
                    else
                    {
                        writer.WriteLine($"{value.Name(idConverter)},");
                    }
                    expectedValue = value.Value + 1;
                }
            }
            writer.WriteLine("}");
        }
Example #2
0
        void WriteEnumCore(FileWriter writer, EnumType enumType, PythonDocCommentWriter docWriter)
        {
            bool mustHaveDocs     = enumType.TypeId != TypeIds.Register && enumType.TypeId != TypeIds.Mnemonic;
            bool uppercaseRawName = PythonUtils.UppercaseEnum(enumType.TypeId.Id1);
            var  firstVersion     = new Version(1, 9, 1);

            // *****************************************************************************
            // For PERF reasons, we do NOT use Enums. They're incredibly slow to load!
            // Eg. loading 'class Code(IntEnum)' (plus other non-Mnemonic enums and some random
            // code) took ~850ms and when I converted them to constants, it took ~43ms!
            // *****************************************************************************
            foreach (var value in enumType.Values)
            {
                if (value.DeprecatedInfo.IsDeprecated && value.DeprecatedInfo.Version < firstVersion)
                {
                    continue;
                }

                var docs = value.Documentation;
                // Sphinx doesn't include the public enum items (global vars in a module) if they're not documented
                if (string.IsNullOrEmpty(docs))
                {
                    if (mustHaveDocs)
                    {
                        throw new InvalidOperationException();
                    }
                    docs = "<no docs>";
                }

                var    numStr = enumType.IsFlags ? NumberFormatter.FormatHexUInt32WithSep(value.Value) : value.Value.ToString();
                string valueName;
                if (uppercaseRawName)
                {
                    valueName = value.RawName.ToUpperInvariant();
                }
                else
                {
                    valueName = value.Name(pythonIdConverter);
                }
                writer.WriteLine($"{valueName}: int = {numStr}");
                if (value.DeprecatedInfo.IsDeprecated)
                {
                    docs = $"DEPRECATED({value.DeprecatedInfo.VersionStr}): {docs}";
                }
                docWriter.WriteSummary(writer, docs, enumType.RawName);
            }
        }
Example #3
0
        void WriteEnumCore(FileWriter writer, PartialEnumFileInfo info, EnumType enumType)
        {
            docWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName);
            var enumTypeName = enumType.Name(idConverter);

            if (enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeWasmBindgen);
            }
            foreach (var attr in info.Attributes)
            {
                writer.WriteLine(attr);
            }
            if (enumType.IsPublic && enumType.IsMissingDocs)
            {
                writer.WriteLine(RustConstants.AttributeAllowMissingDocs);
            }
            if (!enumType.IsPublic)
            {
                writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            }
            var pub = enumType.IsPublic ? "pub " : "pub(crate) ";

            writer.WriteLine($"{pub}enum {enumTypeName} {{");
            using (writer.Indent()) {
                uint expectedValue = 0;
                foreach (var value in enumType.Values)
                {
                    docWriter.WriteSummary(writer, value.Documentation, enumType.RawName);
                    if (enumType.IsFlags)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {NumberFormatter.FormatHexUInt32WithSep(value.Value)},");
                    }
                    else if (expectedValue != value.Value)
                    {
                        writer.WriteLine($"{value.Name(idConverter)} = {value.Value},");
                    }
                    else
                    {
                        writer.WriteLine($"{value.Name(idConverter)},");
                    }
                    expectedValue = value.Value + 1;
                }
            }
            writer.WriteLine("}");
        }
Example #4
0
        string GetValue(Constant constant)
        {
            switch (constant.Kind)
            {
            case ConstantKind.Char:
                var c = (char)constant.ValueUInt64;
                return("'" + c.ToString() + "'");

            case ConstantKind.String:
                if (constant.RefValue is string s)
                {
                    return("\"" + EscapeStringValue(s) + "\"");
                }
                throw new InvalidOperationException();

            case ConstantKind.Int32:
            case ConstantKind.Index:
                if (constant.UseHex)
                {
                    return(NumberFormatter.FormatHexUInt32WithSep((uint)constant.ValueUInt64));
                }
                return(((int)constant.ValueUInt64).ToString());

            case ConstantKind.UInt32:
                if (constant.UseHex)
                {
                    return(NumberFormatter.FormatHexUInt32WithSep((uint)constant.ValueUInt64));
                }
                return(((uint)constant.ValueUInt64).ToString());

            case ConstantKind.UInt64:
                if (constant.UseHex)
                {
                    return(NumberFormatter.FormatHexUInt64WithSep(constant.ValueUInt64));
                }
                return(constant.ValueUInt64.ToString());

            case ConstantKind.Register:
            case ConstantKind.MemorySize:
                return(GetValueString(constant));

            default:
                throw new InvalidOperationException();
            }
        }
Example #5
0
        void WriteEnumRust(FileWriter writer, PartialEnumFileInfo info, EnumType enumType)
        {
            rustDocWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName);
            var enumTypeName = enumType.Name(rustIdConverter);

            foreach (var attr in info.Attributes)
            {
                writer.WriteLine(attr);
            }
            writer.WriteLine(RustConstants.AttributeAllowDeadCode);
            writer.WriteLine($"pub(crate) enum {enumTypeName} {{");
            using (writer.Indent()) {
                uint expectedValue = 0;
                foreach (var value in enumType.Values)
                {
                    if (value.DeprecatedInfo.IsDeprecated)
                    {
                        continue;
                    }
                    rustDocWriter.WriteSummary(writer, value.Documentation, enumType.RawName);
                    if (enumType.IsFlags)
                    {
                        writer.WriteLine($"{value.Name(rustIdConverter)} = {NumberFormatter.FormatHexUInt32WithSep(value.Value)},");
                    }
                    else if (expectedValue != value.Value || enumType.IsPublic)
                    {
                        writer.WriteLine($"{value.Name(rustIdConverter)} = {value.Value},");
                    }
                    else
                    {
                        writer.WriteLine($"{value.Name(rustIdConverter)},");
                    }
                    expectedValue = value.Value + 1;
                }
            }
            writer.WriteLine("}");
        }