Skip to content

Vector35/swift-demangler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swift-demangler

CI crates.io docs.rs

Idiomatic Rust bindings for Swift symbol demangling.

This crate wraps a vendored copy of the Swift runtime's demangling code, providing safe, ergonomic Rust APIs for:

  • Demangling Swift symbols to human-readable strings
  • Traversing the full parse tree with proper lifetime management
  • Extracting function metadata (async, throws, parameters, return type, etc.)

Usage

Add to your project:

cargo add swift-demangler

Simple Demangling

use swift_demangler::demangle;

let demangled = demangle("$s4main5helloSSyYaKF").unwrap();
assert_eq!(demangled, "main.hello() async throws -> Swift.String");

Structured Symbol Analysis

use swift_demangler::{Context, HasFunctionSignature, HasModule, Symbol};

let ctx = Context::new();
if let Some(symbol) = Symbol::parse(&ctx, "$s4main5helloSSyYaKF") {
    if let Some(func) = symbol.as_function() {
        println!("Function: {}", func.name().unwrap_or("?"));
        println!("Module: {}", func.module().unwrap_or("?"));
        println!("Async: {}", func.is_async());
        println!("Throws: {}", func.is_throwing());
    }
}

Node Tree Traversal

For detailed analysis, the raw module provides direct access to the parse tree:

use swift_demangler::Context;
use swift_demangler::raw::{Node, NodeKind};

let ctx = Context::new();
if let Some(root) = Node::parse(&ctx, "$s4main5helloSSyYaKF") {
    for node in root.descendants() {
        println!("{:?}: {:?}", node.kind(), node.text());
    }
}

Building

Default (Bundled)

By default, the crate builds the C++ library from source using CMake:

cargo build

This requires:

  • CMake 3.16+
  • C++20 compiler (Clang or GCC)

Pre-built Library

To link against a pre-built library instead of building from source:

# Build the C library separately
cmake -S swift-demangling -B build -G Ninja
cmake --build build

# Link the Rust crate to the pre-built library
SWIFT_DEMANGLE_DIR=build/lib cargo build --no-default-features

Feature Flags

  • bundled (default) - Build the C++ library from source using CMake
  • cli - Build the swift-demangler command-line tool

CLI Tool

cargo build --features cli --release
./target/release/swift-demangler '$s4main5helloSSyYaKF'
./target/release/swift-demangler --tree '$s4main5helloSSyYaKF'
./target/release/swift-demangler --function '$s4main5helloSSyYaKF'

License

This crate is licensed under the Apache License 2.0.

It includes vendored code from the Swift and LLVM projects:

  • Swift: Apache License 2.0 - see swift-demangling/vendor/LICENSE-Swift.txt
  • LLVM: Apache License 2.0 with LLVM Exceptions - see swift-demangling/vendor/LICENSE-LLVM.txt

About

A Rust crate to demangle Swift symbol names into structured types for analysis

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors