1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci//! rustc demangle for Rust.
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#![feature(rustc_private)]
19800b99b8Sopenharmony_ciextern crate rustc_demangle;
20800b99b8Sopenharmony_ci
21800b99b8Sopenharmony_ciuse std::alloc::{GlobalAlloc, Layout, System};
22800b99b8Sopenharmony_ciuse std::io::Write;
23800b99b8Sopenharmony_ciuse std::os::raw::{c_char};
24800b99b8Sopenharmony_ciuse std::ptr;
25800b99b8Sopenharmony_ciuse std::result::Result::{Ok, Err};
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_ci/// # Safety
28800b99b8Sopenharmony_ci///
29800b99b8Sopenharmony_ci/// C-style interface for demangling.
30800b99b8Sopenharmony_ci#[no_mangle]
31800b99b8Sopenharmony_cipub unsafe extern "C" fn rustc_demangle(mangled: *const c_char) -> *mut c_char
32800b99b8Sopenharmony_ci{
33800b99b8Sopenharmony_ci    if mangled.is_null() {
34800b99b8Sopenharmony_ci        return ptr::null_mut();
35800b99b8Sopenharmony_ci    }
36800b99b8Sopenharmony_ci    let mangled_str = match std::ffi::CStr::from_ptr(mangled).to_str() {
37800b99b8Sopenharmony_ci        Ok(s) => s,
38800b99b8Sopenharmony_ci        Err(_) => return ptr::null_mut(),
39800b99b8Sopenharmony_ci    };
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_ci    match rustc_demangle::try_demangle(mangled_str) {
42800b99b8Sopenharmony_ci        Ok(demangle) => {
43800b99b8Sopenharmony_ci            let size = demangle.to_string().len();
44800b99b8Sopenharmony_ci            let out = unsafe { System.alloc_zeroed(Layout::from_size_align_unchecked(size, 1)) };
45800b99b8Sopenharmony_ci            if out.is_null() {
46800b99b8Sopenharmony_ci                return ptr::null_mut();
47800b99b8Sopenharmony_ci            }
48800b99b8Sopenharmony_ci
49800b99b8Sopenharmony_ci            match write!(unsafe { std::slice::from_raw_parts_mut(out, size) }, "{:#}\0", demangle) {
50800b99b8Sopenharmony_ci                Ok(_) => {
51800b99b8Sopenharmony_ci                    unsafe { std::slice::from_raw_parts_mut(out, size) }.as_mut_ptr() as *mut c_char
52800b99b8Sopenharmony_ci                }
53800b99b8Sopenharmony_ci                Err(_) => ptr::null_mut(),
54800b99b8Sopenharmony_ci            }
55800b99b8Sopenharmony_ci        }
56800b99b8Sopenharmony_ci        Err(_) => ptr::null_mut(),
57800b99b8Sopenharmony_ci    }
58800b99b8Sopenharmony_ci}
59