This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More POD corrections
[perl5.git] / ext / B / B.pm
index f75e54b..91dc5f5 100644 (file)
@@ -6,36 +6,51 @@
 #      License or the Artistic License, as specified in the README file.
 #
 package B;
+use strict;
 
-our $VERSION = '1.01';
-
-use XSLoader ();
 require Exporter;
-@ISA = qw(Exporter);
+@B::ISA = qw(Exporter);
 
 # walkoptree_slow comes from B.pm (you are there),
 # walkoptree comes from B.xs
-@EXPORT_OK = qw(minus_c ppname save_BEGINs
-               class peekop cast_I32 cstring cchar hash threadsv_names
-               main_root main_start main_cv svref_2object opnumber
-               amagic_generation perlstring
-               walkoptree_slow walkoptree walkoptree_exec walksymtable
-               parents comppadlist sv_undef compile_stats timing_info
-               begin_av init_av check_av end_av regex_padav);
-
-sub OPf_KIDS ();
-use strict;
+
+BEGIN {
+    $B::VERSION = '1.28';
+
+    @B::EXPORT_OK = qw(minus_c ppname save_BEGINs
+                      class peekop cast_I32 cstring cchar hash threadsv_names
+                      main_root main_start main_cv svref_2object opnumber
+                      sub_generation amagic_generation perlstring
+                      walkoptree_slow walkoptree walkoptree_exec walksymtable
+                      parents comppadlist sv_undef compile_stats timing_info
+                      begin_av init_av check_av end_av regex_padav dowarn
+                      defstash curstash warnhook diehook inc_gv @optype
+                      @specialsv_name
+                  );
+    push @B::EXPORT_OK, qw(unitcheck_av) if $] > 5.009;
+
+    # All the above in this BEGIN, because our BOOT code needs $VERSION set,
+    # and will append to @EXPORT_OK. And we need to run the BOOT code before
+    # we see OPf_KIDS below.
+    require XSLoader;
+    XSLoader::load();
+}
+
 @B::SV::ISA = 'B::OBJECT';
 @B::NULL::ISA = 'B::SV';
 @B::PV::ISA = 'B::SV';
 @B::IV::ISA = 'B::SV';
-@B::NV::ISA = 'B::IV';
-@B::RV::ISA = 'B::SV';
+@B::NV::ISA = 'B::SV';
+# RV is eliminated with 5.11.0, but effectively is a specialisation of IV now.
+@B::RV::ISA = $] >= 5.011 ? 'B::IV' : 'B::SV';
 @B::PVIV::ISA = qw(B::PV B::IV);
-@B::PVNV::ISA = qw(B::PV B::NV);
+@B::PVNV::ISA = qw(B::PVIV B::NV);
 @B::PVMG::ISA = 'B::PVNV';
-@B::PVLV::ISA = 'B::PVMG';
-@B::BM::ISA = 'B::PVMG';
+@B::REGEXP::ISA = 'B::PVMG' if $] >= 5.011;
+# Change in the inheritance hierarchy post 5.9.0
+@B::PVLV::ISA = $] > 5.009 ? 'B::GV' : 'B::PVMG';
+# BM is eliminated post 5.9.5, but effectively is a specialisation of GV now.
+@B::BM::ISA = $] > 5.009005 ? 'B::GV' : 'B::PVMG';
 @B::AV::ISA = 'B::PVMG';
 @B::GV::ISA = 'B::PVMG';
 @B::HV::ISA = 'B::PVMG';
@@ -51,13 +66,19 @@ use strict;
 @B::SVOP::ISA = 'B::OP';
 @B::PADOP::ISA = 'B::OP';
 @B::PVOP::ISA = 'B::OP';
-@B::CVOP::ISA = 'B::OP';
 @B::LOOP::ISA = 'B::LISTOP';
 @B::PMOP::ISA = 'B::LISTOP';
 @B::COP::ISA = 'B::OP';
 
 @B::SPECIAL::ISA = 'B::OBJECT';
 
+@B::optype = qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP);
+# bytecode.pl contained the following comment:
+# Nullsv *must* come first in the following so that the condition
+# ($$sv == 0) can continue to be used to test (sv == Nullsv).
+@B::specialsv_name = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no
+                       (SV*)pWARN_ALL (SV*)pWARN_NONE (SV*)pWARN_STD);
+
 {
     # Stop "-w" from complaining about the lack of a real B::OBJECT class
     package B::OBJECT;
@@ -84,8 +105,15 @@ sub B::IV::int_value {
 }
 
 sub B::NULL::as_string() {""}
-sub B::IV::as_string()   {goto &B::IV::int_value}
-sub B::PV::as_string()   {goto &B::PV::PV}
+*B::IV::as_string = \&B::IV::int_value;
+*B::PV::as_string = \&B::PV::PV;
+
+#  The input typemap checking makes no distinction between different SV types,
+#  so the XS body will generate the same C code, despite the different XS
+#  "types". So there is no change in behaviour from doing "newXS" like this,
+#  compared with the old approach of having a (near) duplicate XS body.
+#  We should fix the typemap checking.
+*B::IV::RV = \&B::PV::RV if $] > 5.012;
 
 my $debug;
 my $op_count = 0;
@@ -117,7 +145,7 @@ sub walkoptree_slow {
     $op_count++; # just for statistics
     $level ||= 0;
     warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
-    $op->$method($level);
+    $op->$method($level) if $op->can($method);
     if ($$op && ($op->flags & OPf_KIDS)) {
        my $kid;
        unshift(@parents, $op);
@@ -126,7 +154,11 @@ sub walkoptree_slow {
        }
        shift @parents;
     }
-    if (class($op) eq 'PMOP' && $op->pmreplroot && ${$op->pmreplroot}) {
+    if (class($op) eq 'PMOP'
+       && ref($op->pmreplroot)
+       && ${$op->pmreplroot}
+       && $op->pmreplroot->isa( 'B::OP' ))
+    {
        unshift(@parents, $op);
        walkoptree_slow($op->pmreplroot, $method, $level + 1);
        shift @parents;
@@ -223,7 +255,7 @@ sub walksymtable {
         $fullname = "*main::".$prefix.$sym;
        if ($sym =~ /::$/) {
            $sym = $prefix . $sym;
-           if ($sym ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
+           if (svref_2object(\*$sym)->NAME ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
                walksymtable(\%$fullname, $method, $recurse, $sym);
            }
        } else {
@@ -298,15 +330,13 @@ sub walksymtable {
     }
 }
 
-XSLoader::load 'B';
-
 1;
 
 __END__
 
 =head1 NAME
 
-B - The Perl Compiler
+B - The Perl Compiler Backend
 
 =head1 SYNOPSIS
 
@@ -340,7 +370,7 @@ get an initial "handle" on an internal object.
 
 =head2 Functions Returning C<B::SV>, C<B::AV>, C<B::HV>, and C<B::CV> objects
 
-For descriptions of the class hierachy of these objects and the
+For descriptions of the class hierarchy of these objects and the
 methods that can be called on them, see below, L<"OVERVIEW OF
 CLASSES"> and L<"SV-RELATED CLASSES">.
 
@@ -366,11 +396,15 @@ class. Apart from functions such as C<main_root>, this is the primary
 way to get an initial "handle" on an internal perl data structure
 which can then be followed with the other access methods.
 
+The returned object will only be valid as long as the underlying OPs
+and SVs continue to exist. Do not attempt to use the object after the
+underlying structures are freed.
+
 =item amagic_generation
 
 Returns the SV object corresponding to the C variable C<amagic_generation>.
 
-=item C<init_av>
+=item init_av
 
 Returns the AV object (i.e. in class B::AV) representing INIT blocks.
 
@@ -378,6 +412,10 @@ Returns the AV object (i.e. in class B::AV) representing INIT blocks.
 
 Returns the AV object (i.e. in class B::AV) representing CHECK blocks.
 
+=item unitcheck_av
+
+Returns the AV object (i.e. in class B::AV) representing UNITCHECK blocks.
+
 =item begin_av
 
 Returns the AV object (i.e. in class B::AV) representing BEGIN blocks.
@@ -394,7 +432,7 @@ Returns the AV object (i.e. in class B::AV) of the global comppadlist.
 
 Only when perl was compiled with ithreads.
 
-=item C<main_cv>
+=item main_cv
 
 Return the (faked) CV corresponding to the main part of the Perl
 program.
@@ -428,7 +466,7 @@ Methods">, below.
 
 =head2 Functions Returning C<B::OP> objects or for walking op trees
 
-For descriptions of the class hierachy of these objects and the
+For descriptions of the class hierarchy of these objects and the
 methods that can be called on them, see below, L<"OVERVIEW OF
 CLASSES"> and L<"OP-RELATED CLASSES">.
 
@@ -505,7 +543,26 @@ per-thread threadsv variables.
 
 =back
 
+=head2 Exported utility variables
 
+=over 4
+
+=item @optype
+
+  my $op_type = $optype[$op_type_num];
+
+A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
+
+=item @specialsv_name
+
+  my $sv_name = $specialsv_name[$sv_index];
+
+Certain SV types are considered 'special'.  They're represented by
+B::SPECIAL and are referred to by a number from the specialsv_list.
+This array maps that number back to the name of the SV (like 'Nullsv'
+or '&PL_sv_undef').
+
+=back
 
 
 =head1 OVERVIEW OF CLASSES
@@ -521,37 +578,83 @@ The bulk of the C<B> module is the methods for accessing fields of
 these structures.
 
 Note that all access is read-only.  You cannot modify the internals by
-using this module.
+using this module. Also, note that the B::OP and B::SV objects created
+by this module are only valid for as long as the underlying objects
+exist; their creation doesn't increase the reference counts of the
+underlying objects. Trying to access the fields of a freed object will
+give incomprehensible results, or worse.
 
 =head2 SV-RELATED CLASSES
 
-B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM, B::PVLV,
-B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes correspond in
-the obvious way to the underlying C structures of similar names. The
-inheritance hierarchy mimics the underlying C "inheritance":
+B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM (5.9.5 and
+earlier), B::PVLV, B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes
+correspond in the obvious way to the underlying C structures of similar names.
+The inheritance hierarchy mimics the underlying C "inheritance". For the
+5.10.x branch, (I<ie> 5.10.0, 5.10.1 I<etc>) this is:
+
+                           B::SV
+                             |
+                +------------+------------+------------+
+                |            |            |            |
+              B::PV        B::IV        B::NV        B::RV
+                  \         /           /
+                   \       /           /
+                    B::PVIV           /
+                         \           /
+                          \         /
+                           \       /
+                            B::PVNV
+                               |
+                               |
+                            B::PVMG
+                               |
+                   +-----+-----+-----+-----+
+                   |     |     |     |     |
+                 B::AV B::GV B::HV B::CV B::IO
+                         |           |
+                         |           |
+                      B::PVLV      B::FM
+
+For 5.9.0 and earlier, PVLV is a direct subclass of PVMG, and BM is still
+present as a distinct type, so the base of this diagram is
 
-                             B::SV
+
+                               |
+                               |
+                            B::PVMG
+                               |
+            +------+-----+-----+-----+-----+-----+
+            |      |     |     |     |     |     |
+         B::PVLV B::BM B::AV B::GV B::HV B::CV B::IO
+                                           |
+                                           |
+                                         B::FM
+
+For 5.11.0 and later, B::RV is abolished, and IVs can be used to store
+references, and a new type B::REGEXP is introduced, giving this structure:
+
+                           B::SV
+                             |
+                +------------+------------+
+                |            |            |
+              B::PV        B::IV        B::NV
+                  \         /           /
+                   \       /           /
+                    B::PVIV           /
+                         \           /
+                          \         /
+                           \       /
+                            B::PVNV
+                               |
+                               |
+                            B::PVMG
                                |
-                +--------------+----------------------+
-                |              |                      |
-              B::PV          B::IV                  B::RV
-                |  \        /     \
-                |   \      /       \
-                |   B::PVIV         B::NV
-                 \                 /
-                  \____         __/
-                       \       /
-                        B::PVNV
-                           |
-                           |
-                        B::PVMG
-                           |
-         +------+-----+----+------+-----+-----+
-         |      |     |    |      |     |     |
-      B::PVLV B::BM B::AV B::GV B::HV B::CV B::IO
-                                        |
-                                        |
-                                      B::FM
+           +-------+-------+---+---+-------+-------+
+           |       |       |       |       |       |
+         B::AV   B::GV   B::HV   B::CV   B::IO B::REGEXP
+                   |               |
+                   |               |
+                B::PVLV          B::FM
 
 
 Access methods correspond to the underlying C macros for field access,
@@ -569,6 +672,14 @@ C<REFCNT> (corresponding to the C function C<SvREFCNT>).
 
 =item FLAGS
 
+=item object_2svref
+
+Returns a reference to the regular scalar corresponding to this
+B::SV object. In other words, this method is the inverse operation
+to the svref_2object() subroutine. This scalar and other data it points
+at should be considered read-only: modifying them is neither safe nor
+guaranteed to have a sensible effect.
+
 =back
 
 =head2 B::IV Methods
@@ -815,12 +926,23 @@ IoIFP($io) == PerlIO_stdin() ).
 
 =item MAX
 
+=item ARRAY
+
+=item ARRAYelt
+
+Like C<ARRAY>, but takes an index as an argument to get only one element,
+rather than a list of all of them.
+
 =item OFF
 
-=item ARRAY
+This method is deprecated if running under Perl 5.8, and is no longer present
+if running under Perl 5.9
 
 =item AvFLAGS
 
+This method returns the AV specific flags. In Perl 5.9 these are now stored
+in with the main SV flags, so this method is no longer present.
+
 =back
 
 =head2 B::CV Methods
@@ -871,16 +993,19 @@ For constant subroutines, returns the constant SV returned by the subroutine.
 
 =item NAME
 
+=item ARRAY
+
 =item PMROOT
 
-=item ARRAY
+This method is not present if running under Perl 5.9, as the PMROOT
+information is no longer stored directly in the hash.
 
 =back
 
 =head2 OP-RELATED CLASSES
 
 C<B::OP>, C<B::UNOP>, C<B::BINOP>, C<B::LOGOP>, C<B::LISTOP>, C<B::PMOP>,
-C<B::SVOP>, C<B::PADOP>, C<B::PVOP>, C<B::CVOP>, C<B::LOOP>, C<B::COP>.
+C<B::SVOP>, C<B::PADOP>, C<B::PVOP>, C<B::LOOP>, C<B::COP>.
 
 These classes correspond in the obvious way to the underlying C
 structures of similar names. The inheritance hierarchy mimics the
@@ -888,9 +1013,9 @@ underlying C "inheritance":
 
                                  B::OP
                                    |
-                   +---------------+--------+--------+------+
-                   |               |        |        |      |
-                B::UNOP          B::SVOP B::PADOP B::CVOP B::COP
+                   +---------------+--------+--------+-------+
+                   |               |        |        |       |
+                B::UNOP          B::SVOP B::PADOP  B::COP  B::PVOP
                  ,'  `-.
                 /       `--.
            B::BINOP     B::LOGOP
@@ -906,6 +1031,9 @@ with the leading "class indication" prefix (C<"op_">) removed.
 
 =head2 B::OP Methods
 
+These methods get the values of similarly named fields within the OP
+data structure.  See top of C<op.h> for more info.
+
 =over 4
 
 =item next
@@ -930,12 +1058,14 @@ This returns the op description from the global C PL_op_desc array
 
 =item type
 
-=item seq
+=item opt
 
 =item flags
 
 =item private
 
+=item spare
+
 =back
 
 =head2 B::UNOP METHOD
@@ -980,17 +1110,17 @@ This returns the op description from the global C PL_op_desc array
 
 =item pmnext
 
-=item pmregexp
+Only up to Perl 5.9.4
 
 =item pmflags
 
-=item pmdynflags
+=item extflags
 
-=item pmpermflags
+Since Perl 5.9.5
 
 =item precomp
 
-=item pmoffet
+=item pmoffset
 
 Only when perl was compiled with ithreads.
 
@@ -1042,6 +1172,8 @@ Only when perl was compiled with ithreads.
 
 =item stash
 
+=item stashpv
+
 =item file
 
 =item cop_seq
@@ -1050,6 +1182,14 @@ Only when perl was compiled with ithreads.
 
 =item line
 
+=item warnings
+
+=item io
+
+=item hints
+
+=item hints_hash
+
 =back