Skip to content

Commit

Permalink
Improve GNU C++ name mangling
Browse files Browse the repository at this point in the history
This makes it so the symbol is not assumed to be a constructor when it
includes new unless it's the last part of the symbol and infers whether
or not the symbol is a constructor by checking if the last two parts of
it are identical.

This also mangles Str, Pointer, and CArray attributes with the rw or
cpp-const traits properly.

Closes #2884.
  • Loading branch information
Kaiepi authored and AlexDaniel committed May 5, 2020
1 parent 5965cc3 commit 4f672c2
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/NativeCall/Compiler/GNU.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ our sub mangle_cpp_symbol(Routine $r, $symbol) {
$r.signature.set_returns($r.package)
if $r.name eq 'new' && !$r.signature.has_returns && $r.package !~~ GLOBAL;

my $mangled = '_Z'
~ ($r.package.REPR eq 'CPPStruct' ?? 'N' !! '')
~ ($r.?cpp-const ?? 'K' !! '')
~ $symbol.split('::').map({$_ eq 'new' ?? 'C1' !! $_.chars ~ $_}).join('')
~ ($r.package.REPR eq 'CPPStruct' ?? 'E' !! '');
my @params = $r.signature.params;
my $is-cpp-struct = $r.package.REPR eq 'CPPStruct';
my @parts = $symbol.split: '::';
my $mangled = '_Z';
$mangled ~= 'N' if $is-cpp-struct;
$mangled ~= 'K' if $r.?cpp-const;
$mangled ~= .chars ~ $_ for @parts[0..*-2];
if +@parts >= 2 && (@parts.tail eq 'new' || @parts[*-2] eq @parts[*-1]) {
$mangled ~= 'C1';
} else {
$mangled ~= @parts.tail.chars ~ @parts.tail
}
$mangled ~= 'E' if $is-cpp-struct;

my @params = $r.signature.params;
if $r ~~ Method {
@params.shift;
@params.pop if @params.tail.name eq '%_';
}

my $params = join '', @params.map: {
my $R = $_.?cpp-ref ?? 'R' !! ''; # reference
my $P = .rw ?? 'P' !! ''; # pointer
my $K = ($R || $P) && $_.?cpp-const ?? 'K' !! ''; # const
my $R = .?cpp-ref ?? 'R' !! ''; # reference
my $P = .rw ?? 'P' !! ''; # pointer
$P ~= 'P' if .type ~~ Str | NativeCall::Types::Pointer | NativeCall::Types::CArray;
my $K = ($R || $P) && .?cpp-const ?? 'K' !! ''; # const
cpp_param_letter(.type, :$R, :$P, :$K)
};
$mangled ~= $params || 'v';
Expand Down Expand Up @@ -77,10 +86,10 @@ sub cpp_param_letter($type, :$R = '', :$P = '', :$K = '') {
$R ~ $P ~ $K ~ 'd'
}
when Str {
'Pc'
$P ~ $K ~ 'c'
}
when NativeCall::Types::CArray | NativeCall::Types::Pointer {
'P' ~ $K ~ cpp_param_letter(.of);
$P ~ $K ~ cpp_param_letter(.of);
}
default {
my $name = .^name;
Expand Down

0 comments on commit 4f672c2

Please sign in to comment.