@@ -3917,7 +3917,169 @@ statement within a C<CODE> block.
39173917
39183918=head2 Using Typemaps
39193919
3920- XXX TBC
3920+ This section describes the basic facts about using typemaps. For full
3921+ information on creating your own typemaps plus a comprehensive list of
3922+ what standard typemaps are available, see the L<perlxstypemap> document.
3923+
3924+ Typemaps are sets of rules which map C types such as C<int> to logical XS
3925+ types such as C<T_IV>, and from there to C<INPUT> and C<OUTPUT> templates
3926+ such as C<$var = ($type)SvIV($arg);> and C<sv_setiv($arg, (IV)$var);>
3927+ which, after variable expansion, generate C code to convert back and forth
3928+ between Perl arguments and C auto variables.
3929+
3930+ There is a standard system typemap file bundled with Perl for common C and
3931+ Perl types, but in addition, you can add your own typemap file. From Perl
3932+ 5.16.0 onwards you can also include extra typemap declarations in-line
3933+ within the XS file.
3934+
3935+ =head3 Locations and ordering of typemap processing
3936+
3937+ Typemap definitions are processed in order, with more recent entries
3938+ overriding any earlier ones. Definitions are read in first from files and
3939+ then from L<TYPEMAP|/The TYPEMAP: Keyword> sections in the XS file.
3940+
3941+ When considering how files are located and read in, note that the XS
3942+ parser will initially change directory to the directory that contains the
3943+ F<Foo.xs> file that is about to be processed, which will affect any
3944+ subsequent relative paths. Then any typemap files are located and read in.
3945+ The files come from two sources: standard and explicit.
3946+
3947+ Standard typemap files are always called C<typemap> and are searched for
3948+ in a standard set of locations (relative to C<@INC> and to the current
3949+ directory), and any matched files are read in. These paths are, in order
3950+ of processing:
3951+
3952+ "$_/ExtUtils/typemap" for reverse @INC
3953+
3954+ ../../../../lib/ExtUtils/typemap
3955+ ../../../../typemap
3956+ ../../../lib/ExtUtils/typemap
3957+ ../../../typemap
3958+ ../../lib/ExtUtils/typemap
3959+ ../../typemap
3960+ ../lib/ExtUtils/typemap
3961+ ../typemap
3962+ typemap
3963+
3964+ Note that searching C<@INC> in reverse order means that typemap files
3965+ found earlier in C<@INC> are processed later, and thus have higher
3966+ priority.
3967+
3968+ Explicit typemap files are specified either via C<xsubpp -typemap foo ...>
3969+ command line switches, or programmatically by an array passed as:
3970+
3971+ ExtUtils::ParseXS::process_file(..., typemap => ['foo',...]);
3972+
3973+ These files are read in order, and the parser dies if any explicitly
3974+ listed file is not found.
3975+
3976+ Prior to Perl 5.10.0 and Perl 5.8.9, C<@INC> wasn't searched, and standard
3977+ files were searched for and processed I<before> any explicit ones. From
3978+ Perl 5.10.0 onwards, standard files were processed I<after> any explicit
3979+ ones. From Perl 5.44.0 (F<ExtUtils::ParseXS> 3.60) onwards, explicit files
3980+ are again processed last, and thus take priority over standard files.
3981+ In Perl 5.16.0 onwards, C<TYPEMAP> sections are then processed in order
3982+ after any file.
3983+
3984+ Note also that F<ExtUtils::MakeMaker> usually invokes F<xsubpp> with two
3985+ C<-typemap> arguments: the first being the system typemap and the second
3986+ being the module's typemap file, if any. This compensates for older Perls
3987+ not searching C<@INC>.
3988+
3989+ All this complication in fact usually results in, for a typical
3990+ distribution, the typemap file bundled with Perl being read in first, then
3991+ the typemap file included with the distribution adding to (and overriding)
3992+ any standard definitions, then any C<TYPEMAP:> entries in the XS file
3993+ overriding everything.
3994+
3995+ =head3 Reusing, redefining and adding typemap entries
3996+
3997+ Both typemap files and C<TYPEMAP> blocks can have up to three sections:
3998+ C<TYPEMAP> (which is implicit at the start of the file or block) and
3999+ C<INPUT> and C<OUTPUT>. There is no requirement for all three sections to
4000+ be present. Whatever I<is> present is added to the global state for that
4001+ section, either adding a new entry or redefining an existing entry.
4002+
4003+ Probably the simplest use of an additional typemap entry is to map a new C
4004+ type to an I<existing> XS type; for example, given this C type:
4005+
4006+ typedef enum { red, green, blue } colors;
4007+
4008+ then adding the following C-to-XS type-mapping entry to the typemap would
4009+ be sufficient if you just want to treat such enums as simple integers when
4010+ used as parameter and return types:
4011+
4012+ colors T_IV
4013+
4014+ Or you could override just an existing INPUT or OUTPUT template; for
4015+ example:
4016+
4017+ OUTPUT
4018+ T_IV
4019+ my_sv_setiv($arg, (IV)$var);
4020+
4021+ For a completely novel type you might want to add an entry to all three
4022+ sections:
4023+
4024+ foo T_FOO
4025+
4026+ INPUT
4027+ T_FOO
4028+ $var = ($type)get_foo_from_sv($arg);
4029+
4030+ OUTPUT
4031+ T_FOO
4032+ set_sv_to_foo($arg, $var);
4033+
4034+ =head3 Common typemaps
4035+
4036+ This section gives an overview of what common typemap entries are
4037+ available for use. See the L<perlxstypemap> document for a complete list,
4038+ or examine the F<typemap> file which is bundled with the Perl
4039+ distribution. Also, see L</T_PTROBJ and opaque handles> for a detailed
4040+ dive into one particular typemap which is particularly useful for mapping
4041+ between Perl objects and C handles. See L</Returning Values from an XSUB>
4042+ for a general discussion about returning one or more values from an XSUB,
4043+ where typemaps can sometimes be of use (and sometimes aren't).
4044+
4045+ Standard signed C int types such as C<int>, C<long> and C<short>, are all
4046+ mapped to to the C<T_IV> XS type. Integer-like Perl types such C<IV> and
4047+ C<I32> are also mapped to this. If a parameter is declared as something
4048+ mapping to C<T_IV>, then the C<IV> value of the passed SV will be
4049+ extracted (perhaps converting a string value like C<"123"> to an IV), then
4050+ that value will be cast to the final C type, with the usual C rules for
4051+ casting between integer types. Conversely when returning a value, the C
4052+ value is first cast to C<IV>, then the SV is set to that value.
4053+
4054+ Similarly, common C and Perl unsigned types map to C<T_UV>, and values
4055+ are converted back and forth via C<(UV)> casts. A few unsigned types such
4056+ as C<I16> and C<U32> are instead mapped to C<T_U_SHORT> and C<T_U_LONG> XS
4057+ types, but these have the same effect as C<T_UV>.
4058+
4059+ The C<unsigned char> type is treated similarly to other C<T_UV> types, but
4060+ C<char> is treated as a string rather than an integer. A C<char> parameter
4061+ will treat its passed argument as a string and set the auto variable to
4062+ the first I<byte> of that string (which may produce weird results with
4063+ UTF-8 strings). Returning a C<char> value will return a one-character
4064+ string to the Perl caller.
4065+
4066+ The C<char *> type and its common variants are mapped to C<T_PV>. Passed
4067+ parameters will (via C<SvPV()> or similar) return a string buffer
4068+ representing that SV. This buffer may be part of the SV if that SV has a
4069+ string value (or can be converted to a string value), or may be a
4070+ temporary buffer otherwise; for example, an SV holding a reference to an
4071+ array might return a temporary string buffer with the value
4072+ C<"ARRAY(0x12345678)">. When returning the value of a C<T_PV> type, the C
4073+ string is copied to the temporary SV which is to be returned, with its
4074+ length being determined by C<strlen()> or its equivalent. See L</Unicode
4075+ and UTF-8> for the difficulties associated with handling UTF-8 strings.
4076+
4077+ The C<float>, C<double> and C<NV> types map to C<T_FLOAT>, C<T_DOUBLE> and
4078+ C<T_NV> XS types, which all operate by converting to and from an SV via
4079+ C<sv_setnv()> and C<SvNV(sv)> with suitable casting.
4080+
4081+ The C<SV*> type maps to C<T_SV>, which basically does no processing and
4082+ allows you to access the actual passed SV argument.
39214083
39224084=head3 T_PTROBJ and opaque handles
39234085
0 commit comments