1*60869b22SIan Rogers // SPDX-License-Identifier: Apache-2.0 OR MIT 2*60869b22SIan Rogers 3*60869b22SIan Rogers // The contents of this file come from the Rust rustc-demangle library, hosted 4*60869b22SIan Rogers // in the <https://github.com/rust-lang/rustc-demangle> repository, licensed 5*60869b22SIan Rogers // under "Apache-2.0 OR MIT". For copyright details, see 6*60869b22SIan Rogers // <https://github.com/rust-lang/rustc-demangle/blob/main/README.md>. 7*60869b22SIan Rogers // Please note that the file should be kept as close as possible to upstream. 8*60869b22SIan Rogers 9*60869b22SIan Rogers #ifndef _H_DEMANGLE_V0_H 10*60869b22SIan Rogers #define _H_DEMANGLE_V0_H 11*60869b22SIan Rogers 12*60869b22SIan Rogers #ifdef __cplusplus 13*60869b22SIan Rogers extern "C" { 14*60869b22SIan Rogers #endif 15*60869b22SIan Rogers 16*60869b22SIan Rogers #include <stddef.h> 17*60869b22SIan Rogers 18*60869b22SIan Rogers #if defined(__GNUC__) || defined(__clang__) 19*60869b22SIan Rogers #define DEMANGLE_NODISCARD __attribute__((warn_unused_result)) 20*60869b22SIan Rogers #else 21*60869b22SIan Rogers #define DEMANGLE_NODISCARD 22*60869b22SIan Rogers #endif 23*60869b22SIan Rogers 24*60869b22SIan Rogers typedef enum { 25*60869b22SIan Rogers OverflowOk, 26*60869b22SIan Rogers OverflowOverflow 27*60869b22SIan Rogers } overflow_status; 28*60869b22SIan Rogers 29*60869b22SIan Rogers enum demangle_style { 30*60869b22SIan Rogers DemangleStyleUnknown = 0, 31*60869b22SIan Rogers DemangleStyleLegacy, 32*60869b22SIan Rogers DemangleStyleV0, 33*60869b22SIan Rogers }; 34*60869b22SIan Rogers 35*60869b22SIan Rogers // Not using a union here to make the struct easier to copy-paste if needed. 36*60869b22SIan Rogers struct demangle { 37*60869b22SIan Rogers enum demangle_style style; 38*60869b22SIan Rogers // points to the "mangled" part of the name, 39*60869b22SIan Rogers // not including `ZN` or `R` prefixes. 40*60869b22SIan Rogers const char *mangled; 41*60869b22SIan Rogers size_t mangled_len; 42*60869b22SIan Rogers // In DemangleStyleLegacy, is the number of path elements 43*60869b22SIan Rogers size_t elements; 44*60869b22SIan Rogers // while it's called "original", it will not contain `.llvm.9D1C9369@@16` suffixes 45*60869b22SIan Rogers // that are to be ignored. 46*60869b22SIan Rogers const char *original; 47*60869b22SIan Rogers size_t original_len; 48*60869b22SIan Rogers // Contains the part after the mangled name that is to be outputted, 49*60869b22SIan Rogers // which can be `.exit.i.i` suffixes LLVM sometimes adds. 50*60869b22SIan Rogers const char *suffix; 51*60869b22SIan Rogers size_t suffix_len; 52*60869b22SIan Rogers }; 53*60869b22SIan Rogers 54*60869b22SIan Rogers // if the length of the output buffer is less than `output_len-OVERFLOW_MARGIN`, 55*60869b22SIan Rogers // the demangler will return `OverflowOverflow` even if there is no overflow. 56*60869b22SIan Rogers #define OVERFLOW_MARGIN 4 57*60869b22SIan Rogers 58*60869b22SIan Rogers /// Demangle a C string that refers to a Rust symbol and put the demangle intermediate result in `res`. 59*60869b22SIan Rogers /// Beware that `res` contains references into `s`. If `s` is modified (or free'd) before calling 60*60869b22SIan Rogers /// `rust_demangle_display_demangle` behavior is undefined. 61*60869b22SIan Rogers /// 62*60869b22SIan Rogers /// Use `rust_demangle_display_demangle` to convert it to an actual string. 63*60869b22SIan Rogers void rust_demangle_demangle(const char *s, struct demangle *res); 64*60869b22SIan Rogers 65*60869b22SIan Rogers /// Write the string in a `struct demangle` into a buffer. 66*60869b22SIan Rogers /// 67*60869b22SIan Rogers /// Return `OverflowOk` if the output buffer was sufficiently big, `OverflowOverflow` if it wasn't. 68*60869b22SIan Rogers /// This function is `O(n)` in the length of the input + *output* [$], but the demangled output of demangling a symbol can 69*60869b22SIan Rogers /// be exponentially[$$] large, therefore it is recommended to have a sane bound (`rust-demangle` 70*60869b22SIan Rogers /// uses 1,000,000 bytes) on `len`. 71*60869b22SIan Rogers /// 72*60869b22SIan Rogers /// `alternate`, if true, uses the less verbose alternate formatting (Rust `{:#}`) is used, which does not show 73*60869b22SIan Rogers /// symbol hashes and types of constant ints. 74*60869b22SIan Rogers /// 75*60869b22SIan Rogers /// [$] It's `O(n * MAX_DEPTH)`, but `MAX_DEPTH` is a constant 300 and therefore it's `O(n)` 76*60869b22SIan Rogers /// [$$] Technically, bounded by `O(n^MAX_DEPTH)`, but this is practically exponential. 77*60869b22SIan Rogers DEMANGLE_NODISCARD overflow_status rust_demangle_display_demangle(struct demangle const *res, char *out, size_t len, bool alternate); 78*60869b22SIan Rogers 79*60869b22SIan Rogers /// Returns true if `res` refers to a known valid Rust demangling style, false if it's an unknown style. 80*60869b22SIan Rogers bool rust_demangle_is_known(struct demangle *res); 81*60869b22SIan Rogers 82*60869b22SIan Rogers #undef DEMANGLE_NODISCARD 83*60869b22SIan Rogers 84*60869b22SIan Rogers #ifdef __cplusplus 85*60869b22SIan Rogers } 86*60869b22SIan Rogers #endif 87*60869b22SIan Rogers 88*60869b22SIan Rogers #endif 89