Comments (2)
Name Mangling Scheme
The hash values below are made up, but should eventually come from some decent algorithm for short hashes.
- package/module
- Fully qualified name of module is used, with each part mangled individually
- Includes the mangled package name (e.g.
std
) - Hash for each part is created using "mod" + module name
::
becomes__
- Example:
std::num
becomesstd_493ef8a1__num_839a1b34
- type
- Mangled name of containing module +
__
+ mangled name of type - Hash for type is created using "type" + type name
- Example:
std::num::NonZeroU32
becomesstd_493ef8a1__num_839a1b34__NonZeroU32_6743fa12
- Mangled name of containing module +
- method
- Mangled name of type +
__
+ unmangled name of method - Method names are unique for a given type so this works
- Example:
std::num::NonZeroU32::new
becomesstd_493ef8a1__num_839a1b34__NonZeroU32_6743fa12__new
- Mangled name of type +
- function
- Mangled name of containing module/function +
__
+ mangled name of function - Hash for function name is created using "fn" + function name
- Don't want type names and function names to be able to conflict, so both are mangled with a different prefix for the hash
- Example:
std::io::copy
becomesstd_493ef8a1__io_15ad8c91__copy_ab77d31a
- Mangled name of containing module/function +
- variable/function parameter
- Hash of variable name
- Needed to prevent collisions with temporaries
The extra string included in the hash is because different namespaces can contain the same names. That is, you can technically have a type and a function named Foo
.
Short Hashes
use std::io::Read;
use adler32::adler32;
fn mangle_function_name(mod_mangled_name: &str, name: &str) -> String {
let prefix = "fn".as_bytes();
// https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.chain
let mangle_input = prefix.chain(name.as_bytes());
// unwrap() is safe because Read for &[u8] can never result in an error.
let hash = adler32(mangle_input).unwrap();
format!("{}__{}_{:x}", mod_mangled_name, name, hash)
}
Update: Apparently adler32 isn't great for short strings. Might just want to truncate md5 to 32-bits. This may have issues too.
from dino.
Even simpler scheme that just uses the length and not a hash: https://rust-lang.github.io/rfcs/2603-symbol-name-mangling-v2.html#the-mangling-scheme-by-example
from dino.
Related Issues (20)
- Advanced patterns
- Next Steps
- Diagnostics API
- Error Recovery in Parser HOT 2
- Parser Combinators
- Implement casting for numeric types
- Support arbitrary blocks in expr
- Remove distinction between NamedTy and Ty
- Support enums
- Basic Pattern Matching
- Figure out design for Package HOT 1
- Compiled Regular Expression State Machines
- Support Brainfuck Interpreter Sample Program
- Type inference and method/field dispatch HOT 1
- Prevent variables from being used before they're defined
- Block edge case: statements that are valid expressions
- Allow assignment by indexing
- Add x.py script for running all build steps
- Build std using compiler
- Use new cargo environment variables in integration tests
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dino.