Skip to content

Commit 35fc3f0

Browse files
committed
perlxs.pod: add "Using Typemaps" section
Populate this new section (except for the T_PTROBJ subsection, which had already been added by an earlier commit within this branch). Note that the "Common typemaps" subsection could probably benefit from some further expansion by someone familiar with which built-in T_FOO entries are useful.
1 parent 17cf168 commit 35fc3f0

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3959,7 +3959,173 @@ statement within a C<CODE> block.
39593959

39603960
=head2 Using Typemaps
39613961

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

39644130
=head3 T_PTROBJ and opaque handles
39654131

0 commit comments

Comments
 (0)