This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correct precedence problem in #15915
[perl5.git] / cv.h
1 /*    cv.h
2  *
3  *    Copyright (c) 1991-2002, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 /* This structure much match XPVCV in B/C.pm and the beginning of XPVFM
11  * in sv.h  */
12
13 struct xpvcv {
14     char *      xpv_pv;         /* pointer to malloced string */
15     STRLEN      xpv_cur;        /* length of xp_pv as a C string */
16     STRLEN      xpv_len;        /* allocated size */
17     IV          xof_off;        /* integer value */
18     NV          xnv_nv;         /* numeric value, if any */
19     MAGIC*      xmg_magic;      /* magic for scalar array */
20     HV*         xmg_stash;      /* class package */
21
22     HV *        xcv_stash;
23     OP *        xcv_start;
24     OP *        xcv_root;
25     void        (*xcv_xsub) (pTHX_ CV*);
26     ANY         xcv_xsubany;
27     GV *        xcv_gv;
28     char *      xcv_file;
29     long        xcv_depth;      /* >= 2 indicates recursive call */
30     AV *        xcv_padlist;
31     CV *        xcv_outside;
32 #ifdef USE_5005THREADS
33     perl_mutex *xcv_mutexp;
34     struct perl_thread *xcv_owner;      /* current owner thread */
35 #endif /* USE_5005THREADS */
36     cv_flags_t  xcv_flags;
37 };
38
39 /*
40 =head1 Handy Values
41
42 =for apidoc AmU||Nullcv
43 Null CV pointer.
44
45 =head1 CV Manipulation Functions
46
47 =for apidoc Am|HV*|CvSTASH|CV* cv
48 Returns the stash of the CV.
49
50 =cut
51 */
52
53 #define Nullcv Null(CV*)
54
55 #define CvSTASH(sv)     ((XPVCV*)SvANY(sv))->xcv_stash
56 #define CvSTART(sv)     ((XPVCV*)SvANY(sv))->xcv_start
57 #define CvROOT(sv)      ((XPVCV*)SvANY(sv))->xcv_root
58 #define CvXSUB(sv)      ((XPVCV*)SvANY(sv))->xcv_xsub
59 #define CvXSUBANY(sv)   ((XPVCV*)SvANY(sv))->xcv_xsubany
60 #define CvGV(sv)        ((XPVCV*)SvANY(sv))->xcv_gv
61 #define CvFILE(sv)      ((XPVCV*)SvANY(sv))->xcv_file
62 #ifdef USE_ITHREADS
63 #  define CvFILE_set_from_cop(sv, cop)  (CvFILE(sv) = savepv(CopFILE(cop)))
64 #else
65 #  define CvFILE_set_from_cop(sv, cop)  (CvFILE(sv) = CopFILE(cop))
66 #endif
67 #define CvFILEGV(sv)    (gv_fetchfile(CvFILE(sv)))
68 #define CvDEPTH(sv)     ((XPVCV*)SvANY(sv))->xcv_depth
69 #define CvPADLIST(sv)   ((XPVCV*)SvANY(sv))->xcv_padlist
70 #define CvOUTSIDE(sv)   ((XPVCV*)SvANY(sv))->xcv_outside
71 #ifdef USE_5005THREADS
72 #define CvMUTEXP(sv)    ((XPVCV*)SvANY(sv))->xcv_mutexp
73 #define CvOWNER(sv)     ((XPVCV*)SvANY(sv))->xcv_owner
74 #endif /* USE_5005THREADS */
75 #define CvFLAGS(sv)     ((XPVCV*)SvANY(sv))->xcv_flags
76
77 #define CVf_CLONE       0x0001  /* anon CV uses external lexicals */
78 #define CVf_CLONED      0x0002  /* a clone of one of those */
79 #define CVf_ANON        0x0004  /* CvGV() can't be trusted */
80 #define CVf_OLDSTYLE    0x0008
81 #define CVf_UNIQUE      0x0010  /* can't be cloned */
82 #define CVf_NODEBUG     0x0020  /* no DB::sub indirection for this CV
83                                    (esp. useful for special XSUBs) */
84 #define CVf_METHOD      0x0040  /* CV is explicitly marked as a method */
85 #define CVf_LOCKED      0x0080  /* CV locks itself or first arg on entry */
86 #define CVf_LVALUE      0x0100  /* CV return value can be used as lvalue */
87 #define CVf_CONST       0x0200  /* inlinable sub */
88 /* This symbol for optimised communication between toke.c and op.c: */
89 #define CVf_BUILTIN_ATTRS       (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)
90
91 #define CvCLONE(cv)             (CvFLAGS(cv) & CVf_CLONE)
92 #define CvCLONE_on(cv)          (CvFLAGS(cv) |= CVf_CLONE)
93 #define CvCLONE_off(cv)         (CvFLAGS(cv) &= ~CVf_CLONE)
94
95 #define CvCLONED(cv)            (CvFLAGS(cv) & CVf_CLONED)
96 #define CvCLONED_on(cv)         (CvFLAGS(cv) |= CVf_CLONED)
97 #define CvCLONED_off(cv)        (CvFLAGS(cv) &= ~CVf_CLONED)
98
99 #define CvANON(cv)              (CvFLAGS(cv) & CVf_ANON)
100 #define CvANON_on(cv)           (CvFLAGS(cv) |= CVf_ANON)
101 #define CvANON_off(cv)          (CvFLAGS(cv) &= ~CVf_ANON)
102
103 #ifdef PERL_XSUB_OLDSTYLE
104 #define CvOLDSTYLE(cv)          (CvFLAGS(cv) & CVf_OLDSTYLE)
105 #define CvOLDSTYLE_on(cv)       (CvFLAGS(cv) |= CVf_OLDSTYLE)
106 #define CvOLDSTYLE_off(cv)      (CvFLAGS(cv) &= ~CVf_OLDSTYLE)
107 #endif
108
109 #define CvUNIQUE(cv)            (CvFLAGS(cv) & CVf_UNIQUE)
110 #define CvUNIQUE_on(cv)         (CvFLAGS(cv) |= CVf_UNIQUE)
111 #define CvUNIQUE_off(cv)        (CvFLAGS(cv) &= ~CVf_UNIQUE)
112
113 #define CvNODEBUG(cv)           (CvFLAGS(cv) & CVf_NODEBUG)
114 #define CvNODEBUG_on(cv)        (CvFLAGS(cv) |= CVf_NODEBUG)
115 #define CvNODEBUG_off(cv)       (CvFLAGS(cv) &= ~CVf_NODEBUG)
116
117 #define CvMETHOD(cv)            (CvFLAGS(cv) & CVf_METHOD)
118 #define CvMETHOD_on(cv)         (CvFLAGS(cv) |= CVf_METHOD)
119 #define CvMETHOD_off(cv)        (CvFLAGS(cv) &= ~CVf_METHOD)
120
121 #define CvLOCKED(cv)            (CvFLAGS(cv) & CVf_LOCKED)
122 #define CvLOCKED_on(cv)         (CvFLAGS(cv) |= CVf_LOCKED)
123 #define CvLOCKED_off(cv)        (CvFLAGS(cv) &= ~CVf_LOCKED)
124
125 #define CvLVALUE(cv)            (CvFLAGS(cv) & CVf_LVALUE)
126 #define CvLVALUE_on(cv)         (CvFLAGS(cv) |= CVf_LVALUE)
127 #define CvLVALUE_off(cv)        (CvFLAGS(cv) &= ~CVf_LVALUE)
128
129 #define CvEVAL(cv)              (CvUNIQUE(cv) && !SvFAKE(cv))
130 #define CvEVAL_on(cv)           (CvUNIQUE_on(cv),SvFAKE_off(cv))
131 #define CvEVAL_off(cv)          CvUNIQUE_off(cv)
132
133 /* BEGIN|INIT|END */
134 #define CvSPECIAL(cv)           (CvUNIQUE(cv) && SvFAKE(cv))
135 #define CvSPECIAL_on(cv)        (CvUNIQUE_on(cv),SvFAKE_on(cv))
136 #define CvSPECIAL_off(cv)       (CvUNIQUE_off(cv),SvFAKE_off(cv))
137
138 #define CvCONST(cv)             (CvFLAGS(cv) & CVf_CONST)
139 #define CvCONST_on(cv)          (CvFLAGS(cv) |= CVf_CONST)
140 #define CvCONST_off(cv)         (CvFLAGS(cv) &= ~CVf_CONST)
141
142 /*
143 =head1 Pad Data Structures
144
145 =for apidoc m|AV *|CvPADLIST|CV *cv
146 CV's can have CvPADLIST(cv) set to point to an AV.
147
148 For these purposes "forms" are a kind-of CV, eval""s are too (except they're
149 not callable at will and are always thrown away after the eval"" is done
150 executing).
151
152 XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
153 but that is really the callers pad (a slot of which is allocated by
154 every entersub).
155
156 The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items
157 is managed "manual" (mostly in op.c) rather than normal av.c rules.
158 The items in the AV are not SVs as for a normal AV, but other AVs:
159
160 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather
161 the "static type information" for lexicals.
162
163 The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that
164 depth of recursion into the CV.
165 The 0'th slot of a frame AV is an AV which is @_.
166 other entries are storage for variables and op targets.
167
168 During compilation:
169 C<PL_comppad_name> is set the the the names AV.
170 C<PL_comppad> is set the the frame AV for the frame CvDEPTH == 1.
171 C<PL_curpad> is set the body of the frame AV (i.e. AvARRAY(PL_comppad)).
172
173 Itterating over the names AV itterates over all possible pad
174 items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having
175 &PL_sv_undef "names" (see pad_alloc()).
176
177 Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
178 The rest are op targets/GVs/constants which are statically allocated
179 or resolved at compile time.  These don't have names by which they
180 can be looked up from Perl code at run time through eval"" like
181 my/our variables can be.  Since they can't be looked up by "name"
182 but only by their index allocated at compile time (which is usually
183 in PL_op->op_targ), wasting a name SV for them doesn't make sense.
184
185 The SVs in the names AV have their PV being the name of the variable.
186 NV+1..IV inclusive is a range of cop_seq numbers for which the name is valid.
187 For typed lexicals name SV is SVt_PVMG and SvSTASH points at the type.
188
189 If SvFAKE is set on the name SV then slot in the frame AVs are
190 a REFCNT'ed references to a lexical from "outside".
191
192 If the 'name' is '&' the the corresponding entry in frame AV
193 is a CV representing a possible closure.
194 (SvFAKE and name of '&' is not a meaningful combination currently but could
195 become so if C<my sub foo {}> is implemented.)
196
197 =cut
198 */
199