@@ -2083,6 +2083,103 @@ signature.
20832083
20842084=head2 An XSUB Parameter
20852085
2086+ Some examples of valid XSUB parameter declarations:
2087+
2088+ char *foo # parameter with type
2089+ char *foo = "abc" # default value
2090+ char *foo = NO_INIT # doesn't complain if arg missing
2091+ OUT char *foo # caller's arg gets updated
2092+ IN_OUTLIST char *foo # parameter value gets returned
2093+ int length(foo) # pseudo-parameter that gets the length of foo
2094+ foo # placeholder, or parameter without type
2095+ SV* # placeholder
2096+ ... # ellipsis: zero or more further arguments
2097+
2098+ The most straightforward type of declaration in an XSUB's parameter list
2099+ consists of just a C type followed by a parameter name, such as C<char
2100+ *foo>. This has two main effects. First, it causes a C auto variable of
2101+ that name to be declared; and second, the variable is initialised to the
2102+ value of the passed argument which corresponds to that parameter. For
2103+ example,
2104+
2105+ void
2106+ foo(int i, char *s)
2107+
2108+ is roughly equivalent to the Perl:
2109+
2110+ sub foo {
2111+ my $i = int($_[0]);
2112+ my $s = "$_[1]";
2113+ ...
2114+ }
2115+
2116+ and the generated C code may look something like:
2117+
2118+ if (items != 2)
2119+ croak_xs_usage(cv, "i, s");
2120+
2121+ {
2122+ int i = (int)SvIV(ST(0));
2123+ char *s = (char *)SvPV_nolen(ST(1));
2124+ foo(i, s); /* autocall */
2125+ ...
2126+ }
2127+
2128+ In addition to the variable declaration and initialisation, the name of
2129+ the parameter will usually be used in the usage message and in any
2130+ autocall, as shown above. These variables are accessible for any user code
2131+ in a C<CODE> block or similar. Their values aren't normally returned.
2132+
2133+ There are several variations on this basic pattern, which are explained in
2134+ the following subsections.
2135+
2136+ =head3 XSUB Parameter Placeholders
2137+
2138+ Sometimes you want to skip an argument. There are two supported techniques
2139+ for efficiently declaring a placeholder. Both of these will completely
2140+ skip any declaration and initialisation of a C auto variable, but will
2141+ still consume an argument.
2142+
2143+ A bare parameter name is treated as a placeholder if has a name but no
2144+ type specified: neither in the signature, nor in any following C<INPUT>
2145+ section. For example:
2146+
2147+ void
2148+ foo(int a, b, char *c)
2149+ CODE:
2150+ ...
2151+
2152+ is roughly equivalent to the Perl:
2153+
2154+ sub foo {
2155+ my $a = int($_[0]);
2156+ my $c = "$_[2]";
2157+ ...
2158+ }
2159+
2160+ A parameter containing just the specific type C<SV*> and no name is
2161+ treated specially. A bug in the XS parser meant that it used to skip any
2162+ parameter declaration which wasn't parsable. This inadvertently made many
2163+ things de facto placeholder declarations. A common usage was C<SV*>, which
2164+ is now officially treated as a placeholder for backwards compatibility.
2165+ Any other bare types without a parameter name are errors since
2166+ C<ExtUtils::ParseXS> 3.57. Note that the C<SV*> text will appear in any
2167+ C<usage()> error message. For example,
2168+
2169+ void
2170+ foo(int a, SV*, char *c)
2171+
2172+ may croak with:
2173+
2174+ Usage: Foo::Bar::foo(a, SV*, c) at ...
2175+
2176+ Placeholders can't be used with autocall unless you use C<C_ARGS> to
2177+ override the missing argument. For example:
2178+
2179+ void
2180+ foo(int a, b, char *c)
2181+ C_ARGS: a, c
2182+
20862183=head3 The IN/OUTLIST/IN_OUTLIST/OUT/IN_OUT Keywords
20872184
20882185In the list of parameters for an XSUB, one can precede parameter names
0 commit comments