File Coverage

blib/lib/Devel/CallChecker.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 20 20 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Devel::CallChecker - custom op checking attached to subroutines
4              
5             =head1 SYNOPSIS
6              
7             # to generate header prior to XS compilation
8              
9             perl -MDevel::CallChecker=callchecker0_h \
10             -e 'print callchecker0_h' > callchecker0.h
11              
12             # in Perl part of module
13              
14             use Devel::CallChecker;
15              
16             /* in XS */
17              
18             #include "callchecker0.h"
19              
20             cv_get_call_checker(cv, &ckfun, &ckobj);
21             static OP *my_ckfun(pTHX_ OP *o, GV *namegv, SV *ckobj);
22             cv_set_call_checker(cv, my_ckfun, ckobj);
23              
24             =head1 DESCRIPTION
25              
26             This module makes some new features of the Perl 5.14.0 C API available
27             to XS modules running on older versions of Perl. The features are
28             centred around the function C, which allows XS
29             code to attach a magical annotation to a Perl subroutine, resulting in
30             resolvable calls to that subroutine being mutated at compile time by
31             arbitrary C code. This module makes C and several
32             supporting functions available. (It is possible to achieve the effect
33             of C from XS code on much earlier Perl versions,
34             but it is painful to achieve without the centralised facility.)
35              
36             This module provides the implementation of the functions at runtime (on
37             Perls where they are not provided by the core). It also, at compile time,
38             supplies the C header file and link library which provide access to the
39             functions. In normal use, L and L
40             should be called at build time (not authoring time) for the module that
41             wishes to use the C functions.
42              
43             The purpose of this module is specifically to provide the Perl 5.14.0
44             version of the C API to earlier Perl versions where
45             the core doesn't have C at all. This module does not
46             attempt to backport later refinements of the C API.
47             Thus an XS module that uses this module can be sure of having at least
48             the Perl 5.14.0 version of C available, regardless
49             of which Perl version it is running on, but cannot be sure of having
50             any more refined version of the API available. Such a module will have
51             access to the core's version of the API as normal on Perl versions where
52             the core supplies it, and is free to use the ordinary mechanisms of Perl
53             version portability to manage the differences between versions of the API.
54              
55             =cut
56              
57             package Devel::CallChecker;
58              
59 2     2   1187 { use 5.006; }
  2         6  
60 2     2   10 use warnings;
  2         3  
  2         50  
61 2     2   10 use strict;
  2         3  
  2         67  
62              
63             our $VERSION = "0.009";
64              
65 2     2   10 use parent "Exporter";
  2         4  
  2         12  
66             our @EXPORT_OK = qw(callchecker0_h callchecker_linkable);
67              
68             {
69             require DynaLoader;
70             local our @ISA = qw(DynaLoader);
71             local *dl_load_flags = sub { 1 };
72             __PACKAGE__->bootstrap($VERSION);
73             }
74              
75             =head1 CONSTANTS
76              
77             =over
78              
79             =item callchecker0_h
80              
81             Content of a C header file, intended to be named "C".
82             It is to be included in XS code, and C must be included first.
83             When the XS module is loaded at runtime, the C
84             module must be loaded first. This will result in the Perl API functions
85             C, C, C,
86             C, C, and
87             C, as defined below and in the Perl 5.14.0 API,
88             being available to the XS code.
89              
90             =item callchecker_linkable
91              
92             List of names of files that must be used as additional objects when
93             linking an XS module that uses the C functions supplied by this module.
94             This list will be empty on many platforms.
95              
96             =cut
97              
98             sub callchecker_linkable() {
99 2     2 1 3723 require DynaLoader::Functions;
100 2         4701 DynaLoader::Functions->VERSION(0.001);
101 2         11 return DynaLoader::Functions::linkable_for_module(__PACKAGE__);
102             }
103              
104             =back
105              
106             =head1 C FUNCTIONS
107              
108             =over
109              
110             =item rv2cv_op_cv
111              
112             Examines an op, which is expected to identify a subroutine at runtime,
113             and attempts to determine at compile time which subroutine it identifies.
114             This is normally used during Perl compilation to determine whether
115             a prototype can be applied to a function call. I is the op
116             being considered, normally an C op. A pointer to the identified
117             subroutine is returned, if it could be determined statically, and a null
118             pointer is returned if it was not possible to determine statically.
119              
120             Whether the subroutine is statically identifiable is determined in
121             accordance with the prevailing standards of the Perl version being used.
122             The same criteria are used that the core uses to determine whether to
123             apply a prototype to a subroutine call. From version 5.11.2 onwards, the
124             subroutine can be determined if the RV that the C is to operate
125             on is provided by a suitable C or C op. Prior to 5.11.2,
126             only a C op will do. A C op is suitable if the GV's CV slot
127             is populated. A C op is suitable if the constant value is
128             an RV pointing to a CV. Details of this process may change in future
129             versions of Perl.
130              
131             If the C op has the C flag set then no attempt
132             is made to identify the subroutine statically: this flag is used to
133             suppress compile-time magic on a subroutine call, forcing it to use
134             default runtime behaviour.
135              
136             If I has the bit C set, then the handling
137             of a GV reference is modified. If a GV was examined and its CV slot was
138             found to be empty, then the C op has the C flag set.
139             If the op is not optimised away, and the CV slot is later populated with
140             a subroutine having a prototype, that flag eventually triggers the warning
141             "called too early to check prototype".
142              
143             If I has the bit C set, then instead
144             of returning a pointer to the subroutine it returns a pointer to the
145             GV giving the most appropriate name for the subroutine in this context.
146             Normally this is just the C of the subroutine, but for an anonymous
147             (C) subroutine that is referenced through a GV it will be the
148             referencing GV. The resulting C is cast to C to be returned.
149             A null pointer is returned as usual if there is no statically-determinable
150             subroutine.
151              
152             CV *rv2cv_op_cv(OP *cvop, U32 flags)
153              
154             =item cv_get_call_checker
155              
156             Retrieves the function that will be used to fix up a call to I.
157             Specifically, the function is applied to an C op tree for a
158             subroutine call, not marked with C<&>, where the callee can be identified
159             at compile time as I.
160              
161             The C-level function pointer is returned in I<*ckfun_p>, and an SV
162             argument for it is returned in I<*ckobj_p>. The function is intended
163             to be called in this manner:
164              
165             entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
166              
167             In this call, I is a pointer to the C op,
168             which may be replaced by the check function, and I is a GV
169             supplying the name that should be used by the check function to refer
170             to the callee of the C op if it needs to emit any diagnostics.
171             It is permitted to apply the check function in non-standard situations,
172             such as to a call to a different subroutine or to a method call.
173              
174             By default, the function is
175             L,
176             and the SV parameter is I itself. This implements standard
177             prototype processing. It can be changed, for a particular subroutine,
178             by L.
179              
180             void cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p,
181             SV **ckobj_p)
182              
183             =item cv_set_call_checker
184              
185             Sets the function that will be used to fix up a call to I.
186             Specifically, the function is applied to an C op tree for a
187             subroutine call, not marked with C<&>, where the callee can be identified
188             at compile time as I.
189              
190             The C-level function pointer is supplied in I, and an SV argument
191             for it is supplied in I. The function is intended to be called
192             in this manner:
193              
194             entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
195              
196             In this call, I is a pointer to the C op,
197             which may be replaced by the check function, and I is a GV
198             supplying the name that should be used by the check function to refer
199             to the callee of the C op if it needs to emit any diagnostics.
200             It is permitted to apply the check function in non-standard situations,
201             such as to a call to a different subroutine or to a method call.
202              
203             The current setting for a particular CV can be retrieved by
204             L.
205              
206             void cv_set_call_checker(CV *cv, Perl_call_checker ckfun,
207             SV *ckobj)
208              
209             =item ck_entersub_args_list
210              
211             Performs the default fixup of the arguments part of an C
212             op tree. This consists of applying list context to each of the
213             argument ops. This is the standard treatment used on a call marked
214             with C<&>, or a method call, or a call through a subroutine reference,
215             or any other call where the callee can't be identified at compile time,
216             or a call where the callee has no prototype.
217              
218             OP *ck_entersub_args_list(OP *entersubop)
219              
220             =item ck_entersub_args_proto
221              
222             Performs the fixup of the arguments part of an C op tree
223             based on a subroutine prototype. This makes various modifications to
224             the argument ops, from applying context up to inserting C ops,
225             and checking the number and syntactic types of arguments, as directed by
226             the prototype. This is the standard treatment used on a subroutine call,
227             not marked with C<&>, where the callee can be identified at compile time
228             and has a prototype.
229              
230             I supplies the subroutine prototype to be applied to the call.
231             It may be a normal defined scalar, of which the string value will be used.
232             Alternatively, for convenience, it may be a subroutine object (a C
233             that has been cast to C) which has a prototype. The prototype
234             supplied, in whichever form, does not need to match the actual callee
235             referenced by the op tree.
236              
237             If the argument ops disagree with the prototype, for example by having
238             an unacceptable number of arguments, a valid op tree is returned anyway.
239             The error is reflected in the parser state, normally resulting in a single
240             exception at the top level of parsing which covers all the compilation
241             errors that occurred. In the error message, the callee is referred to
242             by the name defined by the I parameter.
243              
244             OP *ck_entersub_args_proto(OP *entersubop, GV *namegv,
245             SV *protosv)
246              
247             =item ck_entersub_args_proto_or_list
248              
249             Performs the fixup of the arguments part of an C op tree either
250             based on a subroutine prototype or using default list-context processing.
251             This is the standard treatment used on a subroutine call, not marked
252             with C<&>, where the callee can be identified at compile time.
253              
254             I supplies the subroutine prototype to be applied to the call,
255             or indicates that there is no prototype. It may be a normal scalar,
256             in which case if it is defined then the string value will be used
257             as a prototype, and if it is undefined then there is no prototype.
258             Alternatively, for convenience, it may be a subroutine object (a C
259             that has been cast to C), of which the prototype will be used if it
260             has one. The prototype (or lack thereof) supplied, in whichever form,
261             does not need to match the actual callee referenced by the op tree.
262              
263             If the argument ops disagree with the prototype, for example by having
264             an unacceptable number of arguments, a valid op tree is returned anyway.
265             The error is reflected in the parser state, normally resulting in a single
266             exception at the top level of parsing which covers all the compilation
267             errors that occurred. In the error message, the callee is referred to
268             by the name defined by the I parameter.
269              
270             OP *ck_entersub_args_proto_or_list(OP *entersubop, GV *namegv,
271             SV *protosv)
272              
273             =back
274              
275             =head1 SEE ALSO
276              
277             L,
278             L,
279             L
280              
281             =head1 AUTHOR
282              
283             Andrew Main (Zefram)
284              
285             =head1 COPYRIGHT
286              
287             Copyright (C) 2011, 2012, 2013, 2015, 2017, 2023
288             Andrew Main (Zefram)
289              
290             =head1 LICENSE
291              
292             This module is free software; you can redistribute it and/or modify it
293             under the same terms as Perl itself.
294              
295             =cut
296              
297             1;