Skip to content

Commit 4d6f204

Browse files
committed
XXX Using Typemaps
write "Using Typemaps" section
1 parent a0f0d01 commit 4d6f204

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,6 +3902,132 @@ statement within a C<CODE> block.
39023902

39033903
=head2 Using Typemaps
39043904

3905+
This section describes the basic facts about using typemaps. For full
3906+
information on creating your own typemaps plus a comprehensive list of
3907+
what standard typemaps are available, see the L<perlxstypemap> document.
3908+
3909+
Typemaps are sets of rules which map C types such as C<int> to logical XS
3910+
types such as C<T_IV>, and from there to C<INPUT> and C<OUTPUT> templates
3911+
such as C<$var = ($type)SvIV($arg);> and C<sv_setiv($arg, (IV)$var);>
3912+
which, after variable expansion, generate C code to convert back and forth
3913+
between Perl arguments and C auto variables.
3914+
3915+
There is a standard system typemap file bundled with Perl for common C and
3916+
Perl types, but you can add your own typemap files in addition. From Perl
3917+
5.16.0 onwards you can also include extra typemap declarations in-line
3918+
within the XS file.
3919+
3920+
=head3 Locations and ordering of typemap processing
3921+
3922+
Typemap definitions are processed in order, with more recent entries
3923+
overriding any earlier ones. Definitions are read in first from files and
3924+
then from L<TYPEMAP|/The TYPEMAP: Keyword> sections in the XS file.
3925+
3926+
When considering how files are located and read in, note that the XS
3927+
parser will initially change directory to the directory that contains the
3928+
F<Foo.xs> file that is about to be processed, which will affect any
3929+
subsequent relative paths. Then any typemap files are located and read in.
3930+
The files come from two sources: standard and explicit.
3931+
3932+
Standard typemap files are always called C<typemap> and are searched for
3933+
in a standard set of locations (relative to both C<@INC> and to the
3934+
current directory), and any matched files are read in. These paths are, in
3935+
order of processing:
3936+
3937+
"$_/ExtUtils/typemap" for reverse @INC
3938+
3939+
../../../../lib/ExtUtils/typemap
3940+
../../../../typemap
3941+
../../../lib/ExtUtils/typemap
3942+
../../../typemap
3943+
../../lib/ExtUtils/typemap
3944+
../../typemap
3945+
../lib/ExtUtils/typemap
3946+
../typemap
3947+
typemap
3948+
3949+
Note that searching C<@INC> in reverse order means that typemap files
3950+
found earlier in C<@INC> are processed later, and thus have higher
3951+
priority.
3952+
3953+
Explicit typemap files are specified either via C<xsubpp -typemap foo ...>
3954+
command line switches, or programmatically by an array passed as:
3955+
3956+
ExtUtils::ParseXS::process_file(..., typemap => ['foo',...]);
3957+
3958+
These files are read in order, and the parser dies if any file is not
3959+
found.
3960+
3961+
Prior to Perl 5.10.0 and Perl 5.8.9, C<@INC> wasn't searched, and standard
3962+
files were searched for and processed I<before> any explicit ones. From
3963+
Perl 5.10.0 onwards, standard files were processed I<after> any explicit
3964+
ones. From Perl 5.44.0 (F<ExtUtils::ParseXS> 3.60) onwards, explicit files
3965+
are again processed last, and thus take priority over standard files.
3966+
In Perl 5.16.0 onwards, C<TYPEMAP> sections are then processed in order
3967+
after any file.
3968+
3969+
Note also that F<ExtUtils::MakeMaker> usually invokes F<xsubpp> with two
3970+
C<-typemap> arguments: the first being the system typemap and the second
3971+
being the module's typemap file, if any. This compensates for older Perls
3972+
not searching C<@INC>.
3973+
3974+
All this complication in fact usually results in, for a typical
3975+
distribution, the typemap file bundled with Perl being read in first, then
3976+
the typemap file included with the distribution adds to (and overrides)
3977+
any standard definitions, then any C<TYPEMAP:> entries in the XS file
3978+
override everything.
3979+
3980+
=head3 Reusing and redefining typemap entries
3981+
3982+
Both typemap files and C<TYPEMAP> blocks can have up to three sections:
3983+
C<TYPEMAP> (which is implicit at the start of the file or block) and
3984+
C<INPUT> and C<OUTPUT>. There is no requirement for all three sections to
3985+
be present. Whatever I<is> present is added to the global state for that
3986+
section, either adding a new entry or redefining an existing entry.
3987+
3988+
Probably the simplest use of an additional typemap entry is to map a new C
3989+
type to an I<existing> XS type; for example, given this C type:
3990+
3991+
typedef enum { red, green, blue } colors;
3992+
3993+
then adding the following C-to-XS type-mapping entry to the typemap would
3994+
be sufficient if you just want to treat such enums as simple integers when
3995+
used as parameter and return types:
3996+
3997+
colors T_IV
3998+
3999+
Or you could override just an existing INPUT or OUTPUT template; for
4000+
example:
4001+
4002+
OUTPUT
4003+
T_IV
4004+
my_sv_setiv($arg, (IV)$var);
4005+
4006+
For a completely novel type you might want to add an entry to all three
4007+
sections:
4008+
4009+
foo T_FOO
4010+
4011+
INPUT
4012+
T_FOO
4013+
$var = ($type)get_foo_from_sv($arg);
4014+
4015+
OUTPUT
4016+
T_FOO
4017+
set_sv_to_foo($arg, $var);
4018+
4019+
=head3 Common typemaps
4020+
4021+
This section gives an overview of what common typemap entries are
4022+
available for use. See the L<perlxstypemap> document for a complete list.
4023+
Also, see L</T_PTROBJ and opaque handles> for a detailed dive into a
4024+
typemap type which is particularly useful for mapping between Perl objects
4025+
and C handles.
4026+
4027+
See also L</Returning Values from an XSUB> for a general discussion about
4028+
returning one or more values from an XSUB, where typemaps can sometimes
4029+
be of use (and sometimes aren't).
4030+
39054031
XXX TBC
39064032

39074033
=head3 T_PTROBJ and opaque handles

0 commit comments

Comments
 (0)