Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a CLI to demangle symbols #960

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
],

products: [
.executable(name: "hc", targets: ["CLI"])
.executable(name: "hc", targets: ["hc"])
],

dependencies: [
Expand Down Expand Up @@ -45,12 +45,19 @@ let package = Package(
targets: [
// The compiler's executable target.
.executableTarget(
name: "CLI",
name: "hc",
dependencies: [
"Driver"
],
swiftSettings: allTargetsSwiftSettings),

.executableTarget(
name: "hylo-demangle",
dependencies: [
"IR"
],
swiftSettings: allTargetsSwiftSettings),

.target(
name: "Driver",
dependencies: [
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions Sources/IR/Mangling/Mangler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ struct Mangler {
/// Writes the mangled representation of `u` to `output`.
private mutating func write(subscriptImpl d: SubscriptImpl.ID, to output: inout Output) {
write(operator: .subscriptImpl, to: &output)
write(base64Didit: program.ast[d].introducer.value, to: &output)
write(base64Digit: program.ast[d].introducer.value, to: &output)
}

/// Writes the mangled representation of `u` to `output`.
Expand Down Expand Up @@ -321,7 +321,7 @@ struct Mangler {
synthesized symbol: SynthesizedFunctionDecl, to output: inout Output
) {
write(operator: .synthesizedFunctionDecl, to: &output)
write(base64Didit: symbol.kind, to: &output)
write(base64Digit: symbol.kind, to: &output)
write(scope: symbol.scope, to: &output)
mangle(type: ^symbol.type, to: &output)
}
Expand Down Expand Up @@ -389,7 +389,7 @@ struct Mangler {

case let t as ParameterType:
write(operator: .parameterType, to: &output)
write(base64Didit: t.access, to: &output)
write(base64Digit: t.access, to: &output)
mangle(type: t.bareType, to: &output)

case let t as ProductType:
Expand All @@ -401,7 +401,7 @@ struct Mangler {

case let t as RemoteType:
write(operator: .remoteType, to: &output)
write(base64Didit: t.access, to: &output)
write(base64Digit: t.access, to: &output)
mangle(type: t.bareType, to: &output)

case let t as SubscriptType:
Expand Down Expand Up @@ -502,7 +502,7 @@ struct Mangler {
/// Writes the mangled representation of `symbol` to `output`.
private mutating func write(subscriptType t: SubscriptType, to output: inout Output) {
write(operator: .subscriptType, to: &output)
write(base64Didit: t.capabilities, to: &output)
write(base64Digit: t.capabilities, to: &output)
mangle(type: t.environment, to: &output)

write(integer: t.inputs.count, to: &output)
Expand Down Expand Up @@ -562,10 +562,10 @@ struct Mangler {

write(base64Didit: tag, to: &output)
if let n = name.notation {
write(base64Didit: n, to: &output)
write(base64Digit: n, to: &output)
}
if let i = name.introducer {
write(base64Didit: i, to: &output)
write(base64Digit: i, to: &output)
}
write(string: name.stem, to: &output)
}
Expand All @@ -583,7 +583,7 @@ struct Mangler {

/// Writes the raw value of `v` encoded as a base 64 digit to `output`.
private func write<T: RawRepresentable>(
base64Didit v: T, to output: inout Output
base64Digit v: T, to output: inout Output
) where T.RawValue == UInt8 {
write(base64Didit: v.rawValue, to: &output)
}
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions Sources/hylo-demangle/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import IR

/// Reports the given `diagnostic` on the standard error and exit with status -1.
func error(_ diagnostic: String) -> Never {
let d = Data("\(diagnostic)\n".utf8)
FileHandle.standardError.write(d)
exit(-1)
}

func main() {
guard CommandLine.arguments.count > 1 else {
error("missing input")
}

let n = CommandLine.arguments[1]
guard let s = DemangledSymbol(n) else {
error("could not demangle '\(n)'")
}

print(s)
}

main()
Loading