1 package ExtUtils::Typemaps::OutputMap;
9 ExtUtils::Typemaps::OutputMap - Entry in the OUTPUT section of a typemap
13 use ExtUtils::Typemaps;
15 my $output = $typemap->get_output_map('T_NV');
16 my $code = $output->code();
21 Refer to L<ExtUtils::Typemaps> for details.
29 Requires C<xstype> and C<code> parameters.
35 my $class = ref($prot)||$prot;
39 if (not defined $args{xstype} or not defined $args{code}) {
40 die("Need xstype and code parameters");
45 (ref($prot) ? {%$prot} : {})
49 $self->{xstype} = $args{xstype} if defined $args{xstype};
50 $self->{code} = $args{code} if defined $args{code};
51 $self->{code} =~ s/^(?=\S)/\t/mg;
58 Returns or sets the OUTPUT mapping code for this entry.
63 $_[0]->{code} = $_[1] if @_ > 1;
69 Returns the name of the XS type of the OUTPUT map.
74 return $_[0]->{xstype};
79 Returns a cleaned-up copy of the code to which certain transformations
80 have been applied to make it more ANSI compliant.
86 my $code = $self->code;
88 # Move C pre-processor instructions to column 1 to be strictly ANSI
89 # conformant. Some pre-processors are fussy about this.
90 $code =~ s/^\s+#/#/mg;
98 This is an obscure but effective optimization that used to
99 live in C<ExtUtils::ParseXS> directly. Not implementing it
100 should never result in incorrect use of typemaps, just less
103 In a nutshell, this will check whether the output code
104 involves calling C<sv_setiv>, C<sv_setuv>, C<sv_setnv>, C<sv_setpv> or
105 C<sv_setpvn> to set the special C<$arg> placeholder to a new value
106 B<AT THE END OF THE OUTPUT CODE>. If that is the case, the code is
107 eligible for using the C<TARG>-related macros to optimize this.
108 Thus the name of the method: C<targetable>.
110 If this optimization is applicable, C<ExtUtils::ParseXS> will
111 emit a C<dXSTARG;> definition at the start of the generate XSUB code,
112 and type (see below) dependent code to set C<TARG> and push it on
113 the stack at the end of the generated XSUB code.
115 If the optimization can not be applied, this returns undef.
116 If it can be applied, this method returns a hash reference containing
117 the following information:
119 type: Any of the characters i, u, n, p
120 with_size: Bool indicating whether this is the sv_setpvn variant
121 what: The code that actually evaluates to the output scalar
122 what_size: If "with_size", this has the string length (as code,
123 not constant, including leading comma)
129 return $self->{targetable} if exists $self->{targetable};
131 our $bal; # ()-balanced
139 my $bal_no_comma = qr[
147 # matches variations on (SV*)
150 \( \s* SV \s* \* \s* \) \s*
154 my $size = qr[ # Third arg (to setpvn)
158 my $code = $self->code;
160 # We can still bootstrap compile 're', because in code re.pm is
161 # available to miniperl, and does not attempt to load the XS code.
164 my ($type, $with_size, $arg, $sarg) =
168 sv_set([iunp])v(n)? # Type, is_setpvn
171 $sv_cast \$arg \s* , \s*
172 ( $bal_no_comma ) # Set from
173 ( $size )? # Possible sizeof set-from
182 with_size => $with_size,
187 $self->{targetable} = $rv;
193 L<ExtUtils::Typemaps>
197 Steffen Mueller C<<smueller@cpan.org>>
199 =head1 COPYRIGHT & LICENSE
201 Copyright 2009, 2010, 2011, 2012 Steffen Mueller
203 This program is free software; you can redistribute it and/or
204 modify it under the same terms as Perl itself.