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 enumTypeName = enumType.Name(pythonIdConverter); var firstVersion = new Version(1, 13, 0); // ***************************************************************************** // 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.GetComment(TargetLanguage.Python); // 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(valueName, numStr) = PythonUtils.GetEnumNameValue(pythonIdConverter, value, uppercaseRawName); writer.WriteLine($"{valueName}: {enumTypeName} = {numStr} # type: ignore"); if (value.DeprecatedInfo.IsDeprecated) { string?extra; if (value.DeprecatedInfo.NewName is not null) { extra = $"Use {value.DeprecatedInfo.NewName} instead"; } else { extra = null; } if (extra is null) { extra = string.Empty; } else { extra = $": {extra}"; } docs = $"DEPRECATED({value.DeprecatedInfo.VersionStr}){extra}"; } docWriter.WriteSummary(writer, docs, enumType.RawName); } }
void WriteFile(FullEnumFileInfo info, EnumType enumType) { var docWriter = new PythonDocCommentWriter(pythonIdConverter, TargetLanguage.Python, isInRootModule: false); using (var writer = new FileWriter(TargetLanguage.Python, FileUtils.OpenWrite(info.Filename))) { writer.WriteFileHeader(); writer.WriteLine("# pylint: disable=invalid-name"); writer.WriteLine("# pylint: disable=line-too-long"); writer.WriteLine("# pylint: disable=too-many-lines"); writer.WriteLine(); docWriter.WriteSummary(writer, enumType.Documentation, enumType.RawName); writer.WriteLine(); WriteEnumCore(writer, enumType, docWriter); } }
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); } }
public PythonInstrCreateGen(GeneratorContext generatorContext) : base(generatorContext.Types) { this.generatorContext = generatorContext; idConverter = PythonIdentifierConverter.Create(); rustIdConverter = RustIdentifierConverter.Create(); docWriter = new PythonDocCommentWriter(idConverter, TargetLanguage.Rust, isInRootModule: true); genNames = new Rust.GenCreateNameArgs { CreatePrefix = "create", Register = "_reg", Memory = "_mem", Int32 = "_i32", UInt32 = "_u32", Int64 = "_i64", UInt64 = "_u64", }; sb = new StringBuilder(); }
void WriteFile(FullEnumFileInfo info, EnumType enumType) { var docWriter = new PythonDocCommentWriter(pythonIdConverter, TargetLanguage.Python, isInRootModule: false); using (var writer = new FileWriter(TargetLanguage.Python, FileUtils.OpenWrite(info.Filename))) { writer.WriteFileHeader(); writer.WriteLine("# pylint: disable=invalid-name"); writer.WriteLine("# pylint: disable=line-too-long"); writer.WriteLine("# pylint: disable=too-many-lines"); writer.WriteLine(); docWriter.WriteSummary(writer, enumType.Documentation.GetComment(TargetLanguage.Python), enumType.RawName); writer.WriteLine(); // Needed by Sphinx or it will generate a lot of errors writer.WriteLine("import typing"); writer.WriteLine("if typing.TYPE_CHECKING:"); using (writer.Indent()) writer.WriteLine($"from ._iced_x86_py import {enumType.Name(pythonIdConverter)}"); writer.WriteLine("else:"); using (writer.Indent()) writer.WriteLine($"{enumType.Name(pythonIdConverter)} = int"); writer.WriteLine(); WriteEnumCore(writer, enumType, docWriter); } }