// Reserve C/C++ keywords and built-in names private static CppNamespace CreateNamespace() { var ns = new CppNamespace(); /* Reserve C/C++ keywords */ foreach (var keyword in new [] { "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "char8_t", "class", "co_await", "co_return", "co_yield", "compl", "concept", "const", "const_cast", "consteval", "constexpr", "constinit", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", "final", "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public", "reflexpr", "register", "reinterpret_cast", "requires", "restrict", "return", "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" }) { ns.ReserveName(keyword); } /* Reserve commonly defined C++ symbols for MSVC DLL projects */ /* This is not an exhaustive list! (windows.h etc.) */ foreach (var symbol in new[] { "_int32", "DEFAULT_CHARSET", "FILETIME", "NULL", "SYSTEMTIME", "stderr", "stdin", "stdout" }) { ns.ReserveName(symbol); } /* Reserve builtin keywords in IDA */ foreach (var keyword in new [] { "_BYTE", "_DWORD", "_OWORD", "_QWORD", "_UNKNOWN", "_WORD", "__array_ptr", "__cdecl", "__cppobj", "__declspec", "__export", "__far", "__fastcall", "__hidden", "__huge", "__import", "__int128", "__int16", "__int32", "__int64", "__int8", "__interrupt", "__near", "__noreturn", "__pascal", "__ptr32", "__ptr64", "__pure", "__restrict", "__return_ptr", "__shifted", "__spoils", "__stdcall", "__struct_ptr", "__thiscall", "__thread", "__unaligned", "__usercall", "__userpurge", "_cs", "_ds", "_es", "_ss", "far", "flat", "near", "Mask", "Region", "Pointer", "GC" }) { ns.ReserveName(keyword); } /* Reserve builtin keywords for Ghidra */ foreach (var keyword in new [] { "_extension" }) { ns.ReserveName(keyword); } return(ns); }
// Generate structure fields for each field of a given type private void GenerateFieldList(CppComplexType type, CppNamespace ns, TypeInfo ti) { var namer = ns.MakeNamer <FieldInfo>(field => field.Name.ToCIdentifier()); foreach (var field in ti.DeclaredFields) { if (field.IsLiteral || field.IsStatic) { continue; } type.AddField(namer.GetName(field), AsCType(field.FieldType)); } }
// Generate structure fields for each field of a given type private void GenerateFieldList(StringBuilder csrc, CppNamespace ns, TypeInfo ti) { var namer = ns.MakeNamer <FieldInfo>((field) => field.Name.ToCIdentifier()); foreach (var field in ti.DeclaredFields) { if (field.IsLiteral || field.IsStatic) { continue; } csrc.Append($" {AsCType(field.FieldType)} {namer.GetName(field)};\n"); } }
public Namer(CppNamespace ns, KeyFunc keyFunc) { this.ns = ns; this.keyFunc = keyFunc; }