This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse: Don’t parenthesize \my @a needlessly
[perl5.git] / lib / ExtUtils / XSSymSet.pm
CommitLineData
ff0cee69 1package ExtUtils::XSSymSet;
2
ff0cee69 3use strict;
84efe3df 4use Config;
ff0cee69 5use vars qw( $VERSION );
93fd879f 6$VERSION = '1.3';
ff0cee69 7
8
9sub new {
10 my($pkg,$maxlen,$silent) = @_;
11 $maxlen ||= 31;
84efe3df
CB
12 # Allow absurdly long symbols here if we've told the compiler to
13 # do the shortening for us.
14 $maxlen = 2048 if $Config{'useshortenedsymbols'};
ff0cee69 15 $silent ||= 0;
16 my($obj) = { '__M@xLen' => $maxlen, '__S!lent' => $silent };
17 bless $obj, $pkg;
18}
19
20
21sub trimsym {
22 my($self,$name,$maxlen,$silent) = @_;
23
24 unless (defined $maxlen) {
25 if (ref $self) { $maxlen ||= $self->{'__M@xLen'}; }
26 $maxlen ||= 31;
27 }
84efe3df
CB
28 $maxlen = 2048 if $Config{'useshortenedsymbols'};
29
ff0cee69 30 unless (defined $silent) {
31 if (ref $self) { $silent ||= $self->{'__S!lent'}; }
32 $silent ||= 0;
33 }
34 return $name if (length $name <= $maxlen);
35
36 my $trimmed = $name;
37 # First, just try to remove duplicated delimiters
38 $trimmed =~ s/__/_/g;
39 if (length $trimmed > $maxlen) {
40 # Next, all duplicated chars
41 $trimmed =~ s/(.)\1+/$1/g;
42 if (length $trimmed > $maxlen) {
43 my $squeezed = $trimmed;
44 my($xs,$prefix,$func) = $trimmed =~ /^(XS_)?(.*)_([^_]*)$/;
b4ff380f
CB
45 $xs ||= '';
46 my $frac = 3; # replaces broken length-based calculations but w/same result
47 my $pat = '([^_])';
ff0cee69 48 if (length $func <= 12) { # Try to preserve short function names
ff0cee69 49 if ($frac > 1) { $pat .= '[^A-Z_]{' . ($frac - 1) . '}'; }
50 $prefix =~ s/$pat/$1/g;
51 $squeezed = "$xs$prefix" . "_$func";
52 if (length $squeezed > $maxlen) {
53 $pat =~ s/A-Z//;
54 $prefix =~ s/$pat/$1/g;
55 $squeezed = "$xs$prefix" . "_$func";
56 }
57 }
58 else {
ff0cee69 59 if ($frac > 1) { $pat .= '[^A-Z_]{' . ($frac - 1) . '}'; }
60 $squeezed = "$prefix$func";
61 $squeezed =~ s/$pat/$1/g;
62 if (length "$xs$squeezed" > $maxlen) {
63 $pat =~ s/A-Z//;
64 $squeezed =~ s/$pat/$1/g;
65 }
66 $squeezed = "$xs$squeezed";
67 }
68 if (length $squeezed <= $maxlen) { $trimmed = $squeezed; }
69 else {
70 my $frac = int((length $trimmed - $maxlen) / length $trimmed + 0.5);
71 my $pat = '(.).{$frac}';
72 $trimmed =~ s/$pat/$1/g;
73 }
74 }
75 }
622db3b8 76 warn "Warning: long symbol $name\n\ttrimmed to $trimmed\n\t" unless $silent;
ff0cee69 77 return $trimmed;
78}
79
80
81sub addsym {
82 my($self,$sym,$maxlen,$silent) = @_;
83 my $trimmed = $self->get_trimmed($sym);
84
85 return $trimmed if defined $trimmed;
86
87 $maxlen ||= $self->{'__M@xLen'} || 31;
88 $silent ||= $self->{'__S!lent'} || 0;
89 $trimmed = $self->trimsym($sym,$maxlen,1);
90 if (exists $self->{$trimmed}) {
91 my($i) = "00";
92 $trimmed = $self->trimsym($sym,$maxlen-3,$silent);
93 while (exists $self->{"${trimmed}_$i"}) { $i++; }
622db3b8 94 warn "Warning: duplicate symbol $trimmed\n\tchanged to ${trimmed}_$i\n\t(original was $sym)\n\t"
ff0cee69 95 unless $silent;
96 $trimmed .= "_$i";
97 }
98 elsif (not $silent and $trimmed ne $sym) {
622db3b8 99 warn "Warning: long symbol $sym\n\ttrimmed to $trimmed\n\t";
ff0cee69 100 }
101 $self->{$trimmed} = $sym;
102 $self->{'__N+Map'}->{$sym} = $trimmed;
103 $trimmed;
104}
105
106
107sub delsym {
108 my($self,$sym) = @_;
109 my $trimmed = $self->{'__N+Map'}->{$sym};
110 if (defined $trimmed) {
111 delete $self->{'__N+Map'}->{$sym};
112 delete $self->{$trimmed};
113 }
114 $trimmed;
115}
116
117
118sub get_trimmed {
119 my($self,$sym) = @_;
120 $self->{'__N+Map'}->{$sym};
121}
122
123
124sub get_orig {
125 my($self,$trimmed) = @_;
126 $self->{$trimmed};
127}
128
129
130sub all_orig { (keys %{$_[0]->{'__N+Map'}}); }
131sub all_trimmed { (grep { /^\w+$/ } keys %{$_[0]}); }
132
133__END__
134
135=head1 NAME
136
57f7515d 137ExtUtils::XSSymSet - keep sets of symbol names palatable to the VMS linker
ff0cee69 138
139=head1 SYNOPSIS
140
57f7515d 141 use ExtUtils::XSSymSet;
ff0cee69 142
57f7515d 143 $set = new ExtUtils::XSSymSet;
ff0cee69 144 while ($sym = make_symbol()) { $set->addsym($sym); }
145 foreach $safesym ($set->all_trimmed) {
555bd962
BG
146 print "Processing $safesym (derived from ",
147 $self->get_orig($safesym), ")\n";
ff0cee69 148 do_stuff($safesym);
149 }
150
57f7515d 151 $safesym = ExtUtils::XSSymSet->trimsym($onesym);
ff0cee69 152
153=head1 DESCRIPTION
154
155Since the VMS linker distinguishes symbols based only on the first 31
156characters of their names, it is occasionally necessary to shorten
157symbol names in order to avoid collisions. (This is especially true of
158names generated by xsubpp, since prefixes generated by nested package
57f7515d 159names can become quite long.) C<ExtUtils::XSSymSet> provides functions to
ff0cee69 160shorten names in a consistent fashion, and to track a set of names to
161insure that each is unique. While designed with F<xsubpp> in mind, it
162may be used with any set of strings.
163
164This package supplies the following functions, all of which should be
165called as methods.
166
167=over 4
168
169=item new([$maxlen[,$silent]])
170
57f7515d 171Creates an empty C<ExtUtils::XSSymset> set of symbols. This function may be
ff0cee69 172called as a static method or via an existing object. If C<$maxlen> or
173C<$silent> are specified, they are used as the defaults for maximum
174name length and warning behavior in future calls to addsym() or
84efe3df
CB
175trimsym() via this object. If the compiler has been instructed to do its
176own symbol shortening via C<$Config{'useshortenedsymbols'}>, a value of
1772048 is assumed for C<$maxlen> as a way of bypassing the shortening done by
178this module.
ff0cee69 179
180=item addsym($name[,$maxlen[,$silent]])
181
182Creates a symbol name from C<$name>, using the methods described
183under trimsym(), which is unique in this set of symbols, and returns
184the new name. C<$name> and its resultant are added to the set, and
185any future calls to addsym() specifying the same C<$name> will return
186the same result, regardless of the value of C<$maxlen> specified.
187Unless C<$silent> is true, warnings are output if C<$name> had to be
188trimmed or changed in order to avoid collision with an existing symbol
189name. C<$maxlen> and C<$silent> default to the values specified when
190this set of symbols was created. This method must be called via an
191existing object.
192
193=item trimsym($name[,$maxlen[,$silent]])
194
195Creates a symbol name C<$maxlen> or fewer characters long from
196C<$name> and returns it. If C<$name> is too long, it first tries to
197shorten it by removing duplicate characters, then by periodically
198removing non-underscore characters, and finally, if necessary, by
199periodically removing characters of any type. C<$maxlen> defaults
200to 31. Unless C<$silent> is true, a warning is output if C<$name>
201is altered in any way. This function may be called either as a
202static method or via an existing object, but in the latter case no
203check is made to insure that the resulting name is unique in the
84efe3df
CB
204set of symbols. If the compiler has been instructed to do its
205own symbol shortening via C<$Config{'useshortenedsymbols'}>, a value
206of 2048 is assumed for C<$maxlen> as a way of bypassing the shortening
207done by this module.
ff0cee69 208
209=item delsym($name)
210
211Removes C<$name> from the set of symbols, where C<$name> is the
212original symbol name passed previously to addsym(). If C<$name>
213existed in the set of symbols, returns its "trimmed" equivalent,
214otherwise returns C<undef>. This method must be called via an
215existing object.
216
217=item get_orig($trimmed)
218
219Returns the original name which was trimmed to C<$trimmed> by a
220previous call to addsym(), or C<undef> if C<$trimmed> does not
221correspond to a member of this set of symbols. This method must be
222called via an existing object.
223
224=item get_trimmed($name)
225
226Returns the trimmed name which was generated from C<$name> by a
227previous call to addsym(), or C<undef> if C<$name> is not a member
228of this set of symbols. This method must be called via an
229existing object.
230
231=item all_orig()
232
233Returns a list containing all of the original symbol names
234from this set.
235
236=item all_trimmed()
237
238Returns a list containing all of the trimmed symbol names
239from this set.
240
241=back
242
243=head1 AUTHOR
244
bd3fa61c 245Charles Bailey E<lt>I<bailey@newman.upenn.edu>E<gt>
ff0cee69 246
247=head1 REVISION
248
84efe3df 249Last revised 8-Oct-2010, for Perl 5.13.6.
ff0cee69 250