sv.h 80.4 KB
Newer Older
1 2
/*    sv.h
 *
3 4
 *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
 *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
5 6 7 8 9 10 11 12 13 14 15
 *
 *    You may distribute under the terms of either the GNU General Public
 *    License or the Artistic License, as specified in the README file.
 *
 */

#ifdef sv_flags
#undef sv_flags		/* Convex has this in <signal.h> for sigvec() */
#endif

/*
16 17
=head1 SV Flags

18
=for apidoc AmU||svtype
19
An enum of flags for Perl types.  These are found in the file B<sv.h>
20 21
in the C<svtype> enum.  Test these flags with the C<SvTYPE> macro.

22 23 24 25 26 27 28 29 30 31
The types are:

    SVt_NULL
    SVt_IV
    SVt_NV
    SVt_RV
    SVt_PV
    SVt_PVIV
    SVt_PVNV
    SVt_PVMG
32
    SVt_INVLIST
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    SVt_REGEXP
    SVt_PVGV
    SVt_PVLV
    SVt_PVAV
    SVt_PVHV
    SVt_PVCV
    SVt_PVFM
    SVt_PVIO

These are most easily explained from the bottom up.

SVt_PVIO is for I/O objects, SVt_PVFM for formats, SVt_PVCV for
subroutines, SVt_PVHV for hashes and SVt_PVAV for arrays.

All the others are scalar types, that is, things that can be bound to a
C<$> variable.  For these, the internal types are mostly orthogonal to
types in the Perl language.

Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
something is a scalar.

SVt_PVGV represents a typeglob.  If !SvFAKE(sv), then it is a real,
incoercible typeglob.  If SvFAKE(sv), then it is a scalar to which a
typeglob has been assigned.  Assigning to it again will stop it from being
a typeglob.  SVt_PVLV represents a scalar that delegates to another scalar
behind the scenes.  It is used, e.g., for the return value of C<substr> and
for tied hash and array elements.  It can hold any scalar value, including
60 61 62
a typeglob.  SVt_REGEXP is for regular
expressions.  SVt_INVLIST is for Perl
core internal use only.
63 64 65 66 67 68 69 70 71 72 73 74 75 76

SVt_PVMG represents a "normal" scalar (not a typeglob, regular expression,
or delegate).  Since most scalars do not need all the internal fields of a
PVMG, we save memory by allocating smaller structs when possible.  All the
other types are just simpler forms of SVt_PVMG, with fewer internal fields.
 SVt_NULL can only hold undef.  SVt_IV can hold undef, an integer, or a
reference.  (SVt_RV is an alias for SVt_IV, which exists for backward
compatibility.)  SVt_NV can hold any of those or a double.  SVt_PV can only
hold undef or a string.  SVt_PVIV is a superset of SVt_PV and SVt_IV.
SVt_PVNV is similar.  SVt_PVMG can hold anything SVt_PVNV can hold, but it
can, but does not have to, be blessed or magical.

=for apidoc AmU||SVt_NULL
Type flag for scalars.  See L</svtype>.
77 78

=for apidoc AmU||SVt_IV
79
Type flag for scalars.  See L</svtype>.
80 81

=for apidoc AmU||SVt_NV
82 83 84 85 86 87 88 89 90 91
Type flag for scalars.  See L</svtype>.

=for apidoc AmU||SVt_PV
Type flag for scalars.  See L</svtype>.

=for apidoc AmU||SVt_PVIV
Type flag for scalars.  See L</svtype>.

=for apidoc AmU||SVt_PVNV
Type flag for scalars.  See L</svtype>.
92 93

=for apidoc AmU||SVt_PVMG
94 95
Type flag for scalars.  See L</svtype>.

96 97 98
=for apidoc AmU||SVt_INVLIST
Type flag for scalars.  See L</svtype>.

99 100 101 102 103 104 105 106
=for apidoc AmU||SVt_REGEXP
Type flag for regular expressions.  See L</svtype>.

=for apidoc AmU||SVt_PVGV
Type flag for typeglobs.  See L</svtype>.

=for apidoc AmU||SVt_PVLV
Type flag for scalars.  See L</svtype>.
107 108

=for apidoc AmU||SVt_PVAV
109
Type flag for arrays.  See L</svtype>.
110 111

=for apidoc AmU||SVt_PVHV
112
Type flag for hashes.  See L</svtype>.
113 114

=for apidoc AmU||SVt_PVCV
115 116 117 118 119 120 121
Type flag for subroutines.  See L</svtype>.

=for apidoc AmU||SVt_PVFM
Type flag for formats.  See L</svtype>.

=for apidoc AmU||SVt_PVIO
Type flag for I/O objects.  See L</svtype>.
122 123

=cut
124 125 126 127 128

  These are ordered so that the simpler types have a lower value; SvUPGRADE
  doesn't allow you to upgrade from a higher numbered type to a lower numbered
  one; also there is code that assumes that anything that has as a PV component
  has a type numbered >= SVt_PV.
129 130
*/

131

132 133
typedef enum {
	SVt_NULL,	/* 0 */
134 135 136
	/* BIND was here, before INVLIST replaced it.  */
	SVt_IV,		/* 1 */
	SVt_NV,		/* 2 */
137
	/* RV was here, before it was merged with IV.  */
138 139
	SVt_PV,		/* 3 */
	SVt_INVLIST,	/* 4, implemented as a PV */
140 141 142 143
	SVt_PVIV,	/* 5 */
	SVt_PVNV,	/* 6 */
	SVt_PVMG,	/* 7 */
	SVt_REGEXP,	/* 8 */
144 145 146 147 148 149
	/* PVBM was here, before BIND replaced it.  */
	SVt_PVGV,	/* 9 */
	SVt_PVLV,	/* 10 */
	SVt_PVAV,	/* 11 */
	SVt_PVHV,	/* 12 */
	SVt_PVCV,	/* 13 */
150
	SVt_PVFM,	/* 14 */
151 152
	SVt_PVIO,	/* 15 */
	SVt_LAST	/* keep last in enum. used to size arrays */
153 154
} svtype;

155
/* *** any alterations to the SV types above need to be reflected in
156 157 158
 * SVt_MASK and the various PL_valid_types_* tables.  As of this writing those
 * tables are in perl.h.  There are also two affected names tables in dump.c,
 * one in B.xs, and 'bodies_by_type[]' in sv.c */
159 160 161

#define SVt_MASK 0xf	/* smallest bitmask that covers all types */

162 163
#ifndef PERL_CORE
/* Although Fast Boyer Moore tables are now being stored in PVGVs, for most
164
   purposes external code wanting to consider PVBM probably needs to think of
165 166
   PVMG instead.  */
#  define SVt_PVBM	SVt_PVMG
167 168 169
/* Anything wanting to create a reference from clean should ensure that it has
   a scalar of type SVt_IV now:  */
#  define SVt_RV	SVt_IV
170 171 172
#endif

/* There is collusion here with sv_clear - sv_clear exits early for SVt_NULL
173 174 175
   so never reaches the clause at the end that uses sv_type_details->body_size
   to determine whether to call safefree(). Hence body_size can be set
   non-zero to record the size of HEs, without fear of bogus frees.  */
176 177 178 179 180 181 182 183 184 185
#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
#define HE_SVSLOT	SVt_NULL
#endif

#define PERL_ARENA_ROOTS_SIZE	(SVt_LAST)

/* typedefs to eliminate some typing */
typedef struct he HE;
typedef struct hek HEK;

186 187
/* Using C's structural equivalence to help emulate C++ inheritance here... */

188 189 190 191 192 193 194 195
/* start with 2 sv-head building blocks */
#define _SV_HEAD(ptrtype) \
    ptrtype	sv_any;		/* pointer to body */	\
    U32		sv_refcnt;	/* how many references to us */	\
    U32		sv_flags	/* what we are */

#define _SV_HEAD_UNION \
    union {				\
196
	char*   svu_pv;		/* pointer to malloced string */	\
197 198 199
	IV      svu_iv;			\
	UV      svu_uv;			\
	SV*     svu_rv;		/* pointer to another SV */		\
200
	struct regexp* svu_rx;		\
201 202 203
	SV**    svu_array;		\
	HE**	svu_hash;		\
	GP*	svu_gp;			\
204
	PerlIO *svu_fp;			\
205 206 207
    }	sv_u


208
struct STRUCT_SV {		/* struct sv { */
209 210 211
    _SV_HEAD(void*);
    _SV_HEAD_UNION;
#ifdef DEBUG_LEAKING_SCALARS
212 213 214
    PERL_BITFIELD32 sv_debug_optype:9;	/* the type of OP that allocated us */
    PERL_BITFIELD32 sv_debug_inpad:1;	/* was allocated in a pad for an OP */
    PERL_BITFIELD32 sv_debug_line:16;	/* the line where we were allocated */
215 216 217
    UV		    sv_debug_serial;	/* serial number of sv allocation   */
    char *	    sv_debug_file;	/* the file where we were allocated */
    SV *	    sv_debug_parent;	/* what we were cloned from (ithreads)*/
218
#endif
219 220 221
};

struct gv {
222 223
    _SV_HEAD(XPVGV*);		/* pointer to xpvgv body */
    _SV_HEAD_UNION;
224 225 226
};

struct cv {
227 228
    _SV_HEAD(XPVCV*);		/* pointer to xpvcv body */
    _SV_HEAD_UNION;
229 230 231
};

struct av {
232 233
    _SV_HEAD(XPVAV*);		/* pointer to xpvav body */
    _SV_HEAD_UNION;
234 235 236
};

struct hv {
237 238
    _SV_HEAD(XPVHV*);		/* pointer to xpvhv body */
    _SV_HEAD_UNION;
239 240 241
};

struct io {
242 243
    _SV_HEAD(XPVIO*);		/* pointer to xpvio body */
    _SV_HEAD_UNION;
244 245
};

246 247 248 249 250
struct p5rx {
    _SV_HEAD(struct regexp*);	/* pointer to regexp body */
    _SV_HEAD_UNION;
};

251 252 253
#undef _SV_HEAD
#undef _SV_HEAD_UNION		/* ensure no pollution */

254
/*
255 256
=head1 SV Manipulation Functions

257 258 259 260
=for apidoc Am|U32|SvREFCNT|SV* sv
Returns the value of the object's reference count.

=for apidoc Am|SV*|SvREFCNT_inc|SV* sv
261
Increments the reference count of the given SV, returning the SV.
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
All of the following SvREFCNT_inc* macros are optimized versions of
SvREFCNT_inc, and can be replaced with SvREFCNT_inc.

=for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
Same as SvREFCNT_inc, but can only be used if you know I<sv>
is not NULL.  Since we don't have to check the NULLness, it's faster
and smaller.

=for apidoc Am|void|SvREFCNT_inc_void|SV* sv
Same as SvREFCNT_inc, but can only be used if you don't need the
return value.  The macro doesn't need to return a meaningful value.

=for apidoc Am|void|SvREFCNT_inc_void_NN|SV* sv
Same as SvREFCNT_inc, but can only be used if you don't need the return
value, and you know that I<sv> is not NULL.  The macro doesn't need
to return a meaningful value, or check for NULLness, so it's smaller
and faster.

=for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
Same as SvREFCNT_inc, but can only be used with expressions without side
effects.  Since we don't have to store a temporary value, it's faster.

=for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
is not NULL.  Since we don't have to check the NULLness, it's faster
and smaller.

=for apidoc Am|void|SvREFCNT_inc_simple_void|SV* sv
Same as SvREFCNT_inc_simple, but can only be used if you don't need the
return value.  The macro doesn't need to return a meaningful value.

=for apidoc Am|void|SvREFCNT_inc_simple_void_NN|SV* sv
Same as SvREFCNT_inc, but can only be used if you don't need the return
value, and you know that I<sv> is not NULL.  The macro doesn't need
to return a meaningful value, or check for NULLness, so it's smaller
and faster.

300
=for apidoc Am|void|SvREFCNT_dec|SV* sv
301
Decrements the reference count of the given SV.  I<sv> may be NULL.
302 303 304 305 306

=for apidoc Am|void|SvREFCNT_dec_NN|SV* sv
Same as SvREFCNT_dec, but can only be used if you know I<sv>
is not NULL.  Since we don't have to check the NULLness, it's faster
and smaller.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

=for apidoc Am|svtype|SvTYPE|SV* sv
Returns the type of the SV.  See C<svtype>.

=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
perform the upgrade if necessary.  See C<svtype>.

=cut
*/

#define SvANY(sv)	(sv)->sv_any
#define SvFLAGS(sv)	(sv)->sv_flags
#define SvREFCNT(sv)	(sv)->sv_refcnt

322 323 324 325
#define SvREFCNT_inc(sv)		S_SvREFCNT_inc(MUTABLE_SV(sv))
#define SvREFCNT_inc_simple(sv)		SvREFCNT_inc(sv)
#define SvREFCNT_inc_NN(sv)		S_SvREFCNT_inc_NN(MUTABLE_SV(sv))
#define SvREFCNT_inc_void(sv)		S_SvREFCNT_inc_void(MUTABLE_SV(sv))
326

327 328
/* These guys don't need the curly blocks */
#define SvREFCNT_inc_simple_void(sv)	STMT_START { if (sv) SvREFCNT(sv)++; } STMT_END
329 330 331
#define SvREFCNT_inc_simple_NN(sv)	(++(SvREFCNT(sv)),MUTABLE_SV(sv))
#define SvREFCNT_inc_void_NN(sv)	(void)(++SvREFCNT(MUTABLE_SV(sv)))
#define SvREFCNT_inc_simple_void_NN(sv)	(void)(++SvREFCNT(MUTABLE_SV(sv)))
332

333 334
#define SvREFCNT_dec(sv)	S_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
#define SvREFCNT_dec_NN(sv)	S_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
335 336

#define SVTYPEMASK	0xff
337 338 339 340 341 342 343
#define SvTYPE(sv)	((svtype)((sv)->sv_flags & SVTYPEMASK))

/* Sadly there are some parts of the core that have pointers to already-freed
   SV heads, and rely on being able to tell that they are now free. So mark
   them all by using a consistent macro.  */
#define SvIS_FREED(sv)	((sv)->sv_flags == SVTYPEMASK)

344 345 346 347
/* this is defined in this peculiar way to avoid compiler warnings.
 * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
#define SvUPGRADE(sv, mt) \
    ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
348 349 350 351 352 353 354 355 356

#define SVf_IOK		0x00000100  /* has valid public integer value */
#define SVf_NOK		0x00000200  /* has valid public numeric value */
#define SVf_POK		0x00000400  /* has valid public pointer value */
#define SVf_ROK		0x00000800  /* has a valid reference pointer */

#define SVp_IOK		0x00001000  /* has valid non-public integer value */
#define SVp_NOK		0x00002000  /* has valid non-public numeric value */
#define SVp_POK		0x00004000  /* has valid non-public pointer value */
357
#define SVp_SCREAM	0x00008000  /* method name is DOES */
358 359 360 361
#define SVphv_CLONEABLE	SVp_SCREAM  /* PVHV (stashes) clone its objects */
#define SVpgv_GP	SVp_SCREAM  /* GV has a valid GP */
#define SVprv_PCS_IMPORTED  SVp_SCREAM  /* RV is a proxy for a constant
				       subroutine in another package. Set the
362
				       GvIMPORTED_CV_on() if it needs to be
363
				       expanded to a real GV */
364
#define SVpad_NAMELIST	SVp_SCREAM  /* AV is a padnamelist */
365 366
#define SVf_IsCOW	0x00010000  /* copy on write (shared hash key if
				       SvLEN == 0) */
367 368 369
#define SVs_PADTMP	0x00020000  /* in use as tmp; only if ! SVs_PADMY */
#define SVs_PADSTALE	0x00020000  /* lexical has gone out of scope;
					only valid for SVs_PADMY */
370 371 372 373 374 375 376 377 378
#define SVpad_TYPED	0x00020000  /* pad name is a Typed Lexical */
#define SVs_PADMY	0x00040000  /* in use a "my" variable */
#define SVpad_OUR	0x00040000  /* pad name is "our" instead of "my" */
#define SVs_TEMP	0x00080000  /* string is stealable? */
#define SVs_OBJECT	0x00100000  /* is "blessed" */
#define SVs_GMG		0x00200000  /* has magical get method */
#define SVs_SMG		0x00400000  /* has magical set method */
#define SVs_RMG		0x00800000  /* has random magical methods */

379
#define SVf_FAKE	0x01000000  /* 0: glob is just a copy
380
				       1: SV head arena wasn't malloc()ed
381
				       2: For PVCV, whether CvUNIQUE(cv)
382 383
					  refers to an eval or once only
					  [CvEVAL(cv), CvSPECIAL(cv)]
384
				       3: On a pad name SV, that slot in the
385 386 387 388 389 390
					  frame AV is a REFCNT'ed reference
					  to a lexical from "outside". */
#define SVf_OOK		0x02000000  /* has valid offset value. For a PVHV this
				       means that a hv_aux struct is present
				       after the main array */
#define SVf_BREAK	0x04000000  /* refcnt is artificially low - used by
391 392 393
				       SVs in final arena cleanup.
				       Set in S_regtry on PL_reg_curpm, so that
				       perl_destruct will skip it. */
394
#define SVf_READONLY	0x08000000  /* may not be modified */
395 396 397 398




399
#define SVf_THINKFIRST	(SVf_READONLY|SVf_ROK|SVf_FAKE|SVs_RMG|SVf_IsCOW)
400 401

#define SVf_OK		(SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
402
			 SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
403

404
#define PRIVSHIFT 4	/* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
405

406
#define SVf_AMAGIC	0x10000000  /* has magical overloaded methods */
407

408 409 410
/* note that SVf_AMAGIC is now only set on stashes, so this bit is free
 * for non-HV SVs */

411 412 413 414 415
/* Ensure this value does not clash with the GV_ADD* flags in gv.h: */
#define SVf_UTF8        0x20000000  /* SvPV is UTF-8 encoded
				       This is also set on RVs whose overloaded
				       stringification is UTF-8. This might
				       only happen as a side effect of SvPV() */
416 417
/* PVHV */
#define SVphv_SHAREKEYS 0x20000000  /* PVHV keys live on shared string table */
418

419 420 421 422
/* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
   be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
   been restructured, so sometimes get used as string buffers.  */

423 424 425

/* Some private flags. */

426 427 428 429 430 431

/* PVNV, PVMG only, and only used in pads. Should be safe to test on any scalar
   SV, as the core is careful to avoid setting both.

   SVf_POK, SVp_POK also set:
   0x00004400   Normal
432
   0x0000C400   method name for DOES (SvSCREAM)
433 434 435 436 437 438 439 440
   0x40004400   FBM compiled (SvVALID)
   0x4000C400   pad name.

   0x00008000   GV with GP
   0x00008800   RV with PCS imported
*/
#define SVpad_NAME	(SVp_SCREAM|SVpbm_VALID)
				    /* This SV is a name in the PAD, so
441 442 443 444 445 446 447 448 449
				       SVpad_TYPED, SVpad_OUR and SVpad_STATE
				       apply */
/* PVAV */
#define SVpav_REAL	0x40000000  /* free old entries */
/* PVHV */
#define SVphv_LAZYDEL	0x40000000  /* entry in xhv_eiter must be deleted */
/* This is only set true on a PVGV when it's playing "PVBM", but is tested for
   on any regular scalar (anything <= PVLV) */
#define SVpbm_VALID	0x40000000
450
/* Only used in toke.c on an SV stored in PL_lex_repl */
451 452 453 454 455 456 457 458 459 460 461 462
#define SVrepl_EVAL	0x40000000  /* Replacement part of s///e */

/* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV  */
#define SVf_IVisUV	0x80000000  /* use XPVUV instead of XPVIV */
/* PVAV */
#define SVpav_REIFY 	0x80000000  /* can become real */
/* PVHV */
#define SVphv_HASKFLAGS	0x80000000  /* keys have flag byte after hash */
/* PVGV when SVpbm_VALID is true */
#define SVpbm_TAIL	0x80000000
/* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
#define SVprv_WEAKREF   0x80000000  /* Weak reference */
463 464
/* pad name vars only */
#define SVpad_STATE	0x80000000  /* pad name is a "state" var */
465

466 467 468
#define _XPV_HEAD							\
    HV*		xmg_stash;	/* class package */			\
    union _xmgu	xmg_u;							\
469
    STRLEN	xpv_cur;	/* length of svu_pv as a C string */    \
470 471 472 473 474 475
    union {								\
	STRLEN	xpvlenu_len; 	/* allocated size */			\
	char *	xpvlenu_pv;	/* regexp string */			\
    } xpv_len_u	

#define xpv_len	xpv_len_u.xpvlenu_len
476 477 478 479 480 481 482 483 484

union _xnvu {
    NV	    xnv_nv;		/* numeric value, if any */
    HV *    xgv_stash;
    struct {
	U32 xlow;
	U32 xhigh;
    }	    xpad_cop_seq;	/* used by pad.c for cop_sequence */
};
485

486 487 488 489
union _xivu {
    IV	    xivu_iv;		/* integer value */
    UV	    xivu_uv;
    HEK *   xivu_namehek;	/* xpvlv, xpvgv: GvNAME */
490 491
};

492 493 494
union _xmgu {
    MAGIC*  xmg_magic;		/* linked list of magicalness */
    HV*	    xmg_ourstash;	/* Stash for our (when SvPAD_OUR is true) */
495
    STRLEN  xmg_hash_index;	/* used while freeing hash entries */
496
};				/* also used by PadnamelistMAXNAMED */
497

498 499
struct xpv {
    _XPV_HEAD;
500 501
};

502 503 504 505
struct xpviv {
    _XPV_HEAD;
    union _xivu xiv_u;
};
506 507 508

#define xiv_iv xiv_u.xivu_iv

509
struct xpvuv {
510 511
    _XPV_HEAD;
    union _xivu xuv_u;
512 513
};

514
#define xuv_uv xuv_u.xivu_uv
515

516
struct xpvnv {
517 518
    _XPV_HEAD;
    union _xivu xiv_u;
519
    union _xnvu xnv_u;
520 521
};

522
/* This structure must match the beginning of struct xpvhv in hv.h. */
523
struct xpvmg {
524
    _XPV_HEAD;
525 526
    union _xivu xiv_u;
    union _xnvu xnv_u;
527 528 529
};

struct xpvlv {
530
    _XPV_HEAD;
531 532
    union _xivu xiv_u;
    union _xnvu xnv_u;
533 534 535 536
    union {
	STRLEN	xlvu_targoff;
	SSize_t xlvu_stargoff;
    } xlv_targoff_u;
537 538
    STRLEN	xlv_targlen;
    SV*		xlv_targ;
539 540
    char	xlv_type;	/* k=keys .=pos x=substr v=vec /=join/re
				 * y=alem/helem/iter t=tie T=tied HE */
541
    char	xlv_flags;	/* 1 = negative offset  2 = negative len */
542 543
};

544 545 546 547 548 549 550 551 552
#define xlv_targoff xlv_targoff_u.xlvu_targoff

struct xpvinvlist {
    _XPV_HEAD;
    IV          prev_index;
    STRLEN	iterator;
    bool	is_offset;	/* */
};

553 554
/* This structure works in 3 ways - regular scalar, GV with GP, or fast
   Boyer-Moore.  */
555
struct xpvgv {
556
    _XPV_HEAD;
557 558
    union _xivu xiv_u;
    union _xnvu xnv_u;
559 560
};

561
typedef U32 cv_flags_t;
562

563 564 565 566 567 568 569 570 571 572
#define _XPVCV_COMMON								\
    HV *	xcv_stash;							\
    union {									\
	OP *	xcv_start;							\
	ANY	xcv_xsubany;							\
    }		xcv_start_u;					    		\
    union {									\
	OP *	xcv_root;							\
	void	(*xcv_xsub) (pTHX_ CV*);					\
    }		xcv_root_u;							\
573 574 575 576
    union {								\
	GV *	xcv_gv;							\
	HEK *	xcv_hek;						\
    }		xcv_gv_u;						\
577
    char *	xcv_file;							\
578
    PADLIST *	xcv_padlist;							\
579 580 581 582
    CV *	xcv_outside;							\
    U32		xcv_outside_seq; /* the COP sequence (at the point of our	\
				  * compilation) in the lexically enclosing	\
				  * sub */					\
583 584 585 586
    cv_flags_t	xcv_flags;						\
    I32	xcv_depth	/* >= 2 indicates recursive call */

/* This structure must match XPVCV in cv.h */
587

588
struct xpvfm {
589 590
    _XPV_HEAD;
    _XPVCV_COMMON;
591 592
};

593

594
struct xpvio {
595
    _XPV_HEAD;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    union _xivu xiv_u;
    /* ifp and ofp are normally the same, but sockets need separate streams */
    PerlIO *	xio_ofp;
    /* Cray addresses everything by word boundaries (64 bits) and
     * code and data pointers cannot be mixed (which is exactly what
     * Perl_filter_add() tries to do with the dirp), hence the
     *  following union trick (as suggested by Gurusamy Sarathy).
     * For further information see Geir Johansen's problem report
     * titled [ID 20000612.002] Perl problem on Cray system
     * The any pointer (known as IoANY()) will also be a good place
     * to hang any IO disciplines to.
     */
    union {
	DIR *	xiou_dirp;	/* for opendir, readdir, etc */
	void *	xiou_any;	/* for alignment */
    } xio_dirpu;
    /* IV xio_lines is now in IVX  $. */
    IV		xio_page;	/* $% */
    IV		xio_page_len;	/* $= */
    IV		xio_lines_left;	/* $- */
    char *	xio_top_name;	/* $^ */
    GV *	xio_top_gv;	/* $^ */
    char *	xio_fmt_name;	/* $~ */
    GV *	xio_fmt_gv;	/* $~ */
    char *	xio_bottom_name;/* $^B */
    GV *	xio_bottom_gv;	/* $^B */
    char	xio_type;
    U8		xio_flags;
624
};
625

626 627 628 629 630 631 632 633 634
#define xio_dirp	xio_dirpu.xiou_dirp
#define xio_any		xio_dirpu.xiou_any

#define IOf_ARGV	1	/* this fp iterates over ARGV */
#define IOf_START	2	/* check for null ARGV and substitute '-' */
#define IOf_FLUSH	4	/* this fp wants a flush after write op */
#define IOf_DIDTOP	8	/* just did top of form */
#define IOf_UNTAINT	16	/* consider this fp (and its data) "safe" */
#define IOf_NOLINE	32	/* slurped a pseudo-line from empty file */
635 636
#define IOf_FAKE_DIRP	64	/* xio_dirp is fake (source filters kludge)
				   Also, when this is set, SvPVX() is valid */
637 638 639 640

/* The following macros define implementation-independent predicates on SVs. */

/*
641 642
=for apidoc Am|U32|SvNIOK|SV* sv
Returns a U32 value indicating whether the SV contains a number, integer or
643 644
double.

645 646
=for apidoc Am|U32|SvNIOKp|SV* sv
Returns a U32 value indicating whether the SV contains a number, integer or
647
double.  Checks the B<private> setting.  Use C<SvNIOK> instead.
648 649 650 651

=for apidoc Am|void|SvNIOK_off|SV* sv
Unsets the NV/IV status of an SV.

652
=for apidoc Am|U32|SvOK|SV* sv
653
Returns a U32 value indicating whether the value is defined.  This is
654
only meaningful for scalars.
655

656 657
=for apidoc Am|U32|SvIOKp|SV* sv
Returns a U32 value indicating whether the SV contains an integer.  Checks
658
the B<private> setting.  Use C<SvIOK> instead.
659

660 661
=for apidoc Am|U32|SvNOKp|SV* sv
Returns a U32 value indicating whether the SV contains a double.  Checks the
662
B<private> setting.  Use C<SvNOK> instead.
663

664 665
=for apidoc Am|U32|SvPOKp|SV* sv
Returns a U32 value indicating whether the SV contains a character string.
666
Checks the B<private> setting.  Use C<SvPOK> instead.
667

668 669
=for apidoc Am|U32|SvIOK|SV* sv
Returns a U32 value indicating whether the SV contains an integer.
670 671 672 673 674 675 676 677 678 679 680

=for apidoc Am|void|SvIOK_on|SV* sv
Tells an SV that it is an integer.

=for apidoc Am|void|SvIOK_off|SV* sv
Unsets the IV status of an SV.

=for apidoc Am|void|SvIOK_only|SV* sv
Tells an SV that it is an integer and disables all other OK bits.

=for apidoc Am|void|SvIOK_only_UV|SV* sv
681
Tells an SV that it is an unsigned integer and disables all other OK bits.
682

683
=for apidoc Am|bool|SvIOK_UV|SV* sv
684 685 686
Returns a boolean indicating whether the SV contains an integer that must be
interpreted as unsigned.  A non-negative integer whose value is within the
range of both an IV and a UV may be be flagged as either SvUOK or SVIOK.
687

688
=for apidoc Am|bool|SvUOK|SV* sv
689 690 691
Returns a boolean indicating whether the SV contains an integer that must be
interpreted as unsigned.  A non-negative integer whose value is within the
range of both an IV and a UV may be be flagged as either SvUOK or SVIOK.
692

693 694
=for apidoc Am|bool|SvIOK_notUV|SV* sv
Returns a boolean indicating whether the SV contains a signed integer.
695

696 697
=for apidoc Am|U32|SvNOK|SV* sv
Returns a U32 value indicating whether the SV contains a double.
698 699 700 701 702 703 704 705 706 707

=for apidoc Am|void|SvNOK_on|SV* sv
Tells an SV that it is a double.

=for apidoc Am|void|SvNOK_off|SV* sv
Unsets the NV status of an SV.

=for apidoc Am|void|SvNOK_only|SV* sv
Tells an SV that it is a double and disables all other OK bits.

708 709
=for apidoc Am|U32|SvPOK|SV* sv
Returns a U32 value indicating whether the SV contains a character
710 711 712 713 714 715 716 717 718 719
string.

=for apidoc Am|void|SvPOK_on|SV* sv
Tells an SV that it is a string.

=for apidoc Am|void|SvPOK_off|SV* sv
Unsets the PV status of an SV.

=for apidoc Am|void|SvPOK_only|SV* sv
Tells an SV that it is a string and disables all other OK bits.
720
Will also turn off the UTF-8 status.
721

722 723 724 725
=for apidoc Am|bool|SvVOK|SV* sv
Returns a boolean indicating whether the SV contains a v-string.

=for apidoc Am|U32|SvOOK|SV* sv
726 727 728 729 730 731
Returns a U32 indicating whether the pointer to the string buffer is offset.
This hack is used internally to speed up removal of characters from the
beginning of a SvPV.  When SvOOK is true, then the start of the
allocated string buffer is actually C<SvOOK_offset()> bytes before SvPVX.
This offset used to be stored in SvIVX, but is now stored within the spare
part of the buffer.
732

733
=for apidoc Am|U32|SvROK|SV* sv
734 735 736 737 738 739 740 741 742 743 744 745
Tests if the SV is an RV.

=for apidoc Am|void|SvROK_on|SV* sv
Tells an SV that it is an RV.

=for apidoc Am|void|SvROK_off|SV* sv
Unsets the RV status of an SV.

=for apidoc Am|SV*|SvRV|SV* sv
Dereferences an RV to return the SV.

=for apidoc Am|IV|SvIVX|SV* sv
746
Returns the raw value in the SV's IV slot, without checks or conversions.
747
Only use when you are sure SvIOK is true.  See also C<SvIV()>.
748 749

=for apidoc Am|UV|SvUVX|SV* sv
750
Returns the raw value in the SV's UV slot, without checks or conversions.
751
Only use when you are sure SvIOK is true.  See also C<SvUV()>.
752 753

=for apidoc Am|NV|SvNVX|SV* sv
754
Returns the raw value in the SV's NV slot, without checks or conversions.
755
Only use when you are sure SvNOK is true.  See also C<SvNV()>.
756 757

=for apidoc Am|char*|SvPVX|SV* sv
758
Returns a pointer to the physical string in the SV.  The SV must contain a
759 760
string.  Prior to 5.9.3 it is not safe
to execute this macro unless the SV's
761
type >= SVt_PV.
762

763 764 765
This is also used to store the name of an autoloaded subroutine in an XS
AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.

766 767 768 769 770 771 772 773
=for apidoc Am|STRLEN|SvCUR|SV* sv
Returns the length of the string which is in the SV.  See C<SvLEN>.

=for apidoc Am|STRLEN|SvLEN|SV* sv
Returns the size of the string buffer in the SV, not including any part
attributable to C<SvOOK>.  See C<SvCUR>.

=for apidoc Am|char*|SvEND|SV* sv
774 775
Returns a pointer to the spot just after the last character in
the string which is in the SV, where there is usually a trailing
776
C<NUL> character (even though Perl scalars do not strictly require it).
777 778
See C<SvCUR>.  Access the character as *(SvEND(sv)).

779 780 781
Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
unallocated memory.

782 783 784
=for apidoc Am|HV*|SvSTASH|SV* sv
Returns the stash of the SV.

785 786 787 788 789 790 791 792 793 794
=for apidoc Am|void|SvIV_set|SV* sv|IV val
Set the value of the IV pointer in sv to val.  It is possible to perform
the same function of this macro with an lvalue assignment to C<SvIVX>.
With future Perls, however, it will be more efficient to use 
C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.

=for apidoc Am|void|SvNV_set|SV* sv|NV val
Set the value of the NV pointer in sv to val.  See C<SvIV_set>.

=for apidoc Am|void|SvPV_set|SV* sv|char* val
795 796
This is probably not what you want to use, you probably wanted
L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
797

798 799 800 801
Set the value of the PV pointer in C<sv> to the Perl allocated
C<NUL>-terminated string C<val>.  See also C<SvIV_set>.

Remember to free the previous PV buffer. There are many things to check.
802 803 804
Beware that the existing pointer may be involved in copy-on-write or other
mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
C<SvPV_force> (or check the SvIsCOW flag) first to make sure this
805 806
modification is safe. Then finally, if it is not a COW, call C<SvPV_free> to
free the previous PV buffer.
807 808 809 810 811 812 813 814 815 816

=for apidoc Am|void|SvUV_set|SV* sv|UV val
Set the value of the UV pointer in sv to val.  See C<SvIV_set>.

=for apidoc Am|void|SvRV_set|SV* sv|SV* val
Set the value of the RV pointer in sv to val.  See C<SvIV_set>.

=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.

817
=for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
818 819
Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.

820
=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
821 822 823 824 825
Set the current length of the string which is in the SV.  See C<SvCUR>
and C<SvIV_set>.

=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
Set the actual length of the string which is in the SV.  See C<SvIV_set>.
826 827 828 829 830 831 832 833 834

=cut
*/

#define SvNIOK(sv)		(SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
#define SvNIOKp(sv)		(SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
#define SvNIOK_off(sv)		(SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
						  SVp_IOK|SVp_NOK|SVf_IVisUV))

835 836
#define assert_not_ROK(sv)	assert_(!SvROK(sv) || !SvRV(sv))
#define assert_not_glob(sv)	assert_(!isGV_with_GP(sv))
837

838
#define SvOK(sv)		(SvFLAGS(sv) & SVf_OK || isREGEXP(sv))
839 840
#define SvOK_off(sv)		(assert_not_ROK(sv) assert_not_glob(sv)	\
				 SvFLAGS(sv) &=	~(SVf_OK|		\
841 842
						  SVf_IVisUV|SVf_UTF8),	\
							SvOOK_off(sv))
843
#define SvOK_off_exc_UV(sv)	(assert_not_ROK(sv)			\
844
				 SvFLAGS(sv) &=	~(SVf_OK|		\
845 846 847 848 849
						  SVf_UTF8),		\
							SvOOK_off(sv))

#define SvOKp(sv)		(SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
#define SvIOKp(sv)		(SvFLAGS(sv) & SVp_IOK)
850
#define SvIOKp_on(sv)		(assert_not_glob(sv) SvRELEASE_IVX_(sv)	\
851
				    SvFLAGS(sv) |= SVp_IOK)
852
#define SvNOKp(sv)		(SvFLAGS(sv) & SVp_NOK)
853
#define SvNOKp_on(sv)		(assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
854
#define SvPOKp(sv)		(SvFLAGS(sv) & SVp_POK)
855
#define SvPOKp_on(sv)		(assert_not_ROK(sv) assert_not_glob(sv)	\
856
				 SvFLAGS(sv) |= SVp_POK)
857 858

#define SvIOK(sv)		(SvFLAGS(sv) & SVf_IOK)
859
#define SvIOK_on(sv)		(assert_not_glob(sv) SvRELEASE_IVX_(sv)	\
860 861
				    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
#define SvIOK_off(sv)		(SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
862
#define SvIOK_only(sv)		(SvOK_off(sv), \
863
				    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
864
#define SvIOK_only_UV(sv)	(assert_not_glob(sv) SvOK_off_exc_UV(sv), \
865 866 867 868
				    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))

#define SvIOK_UV(sv)		((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))	\
				 == (SVf_IOK|SVf_IVisUV))
869
#define SvUOK(sv)		SvIOK_UV(sv)
870 871 872 873 874 875 876 877
#define SvIOK_notUV(sv)		((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))	\
				 == SVf_IOK)

#define SvIsUV(sv)		(SvFLAGS(sv) & SVf_IVisUV)
#define SvIsUV_on(sv)		(SvFLAGS(sv) |= SVf_IVisUV)
#define SvIsUV_off(sv)		(SvFLAGS(sv) &= ~SVf_IVisUV)

#define SvNOK(sv)		(SvFLAGS(sv) & SVf_NOK)
878 879
#define SvNOK_on(sv)		(assert_not_glob(sv) \
				 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
880
#define SvNOK_off(sv)		(SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
881
#define SvNOK_only(sv)		(SvOK_off(sv), \
882 883 884
				    SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))

/*
885
=for apidoc Am|U32|SvUTF8|SV* sv
886 887
Returns a U32 value indicating the UTF-8 status of an SV.  If things are set-up
properly, this indicates whether or not the SV contains UTF-8 encoded data.
888 889
You should use this I<after> a call to SvPV() or one of its variants, in
case any call to string overloading updates the internal flag.
890 891

=for apidoc Am|void|SvUTF8_on|SV *sv
892 893
Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
Do not use frivolously.
894 895

=for apidoc Am|void|SvUTF8_off|SV *sv
896 897
Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
Do not use frivolously.
898 899

=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
900 901 902
Tells an SV that it is a string and disables all other OK bits,
and leaves the UTF-8 status as it was.

903 904 905
=cut
 */

906 907
/* Ensure the return value of this macro does not clash with the GV_ADD* flags
in gv.h: */
908 909 910 911 912
#define SvUTF8(sv)		(SvFLAGS(sv) & SVf_UTF8)
#define SvUTF8_on(sv)		(SvFLAGS(sv) |= (SVf_UTF8))
#define SvUTF8_off(sv)		(SvFLAGS(sv) &= ~(SVf_UTF8))

#define SvPOK(sv)		(SvFLAGS(sv) & SVf_POK)
913
#define SvPOK_on(sv)		(assert_not_ROK(sv) assert_not_glob(sv)	\
914
				 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
915
#define SvPOK_off(sv)		(SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
916 917
#define SvPOK_only(sv)		(assert_not_ROK(sv) assert_not_glob(sv)	\
				 SvFLAGS(sv) &= ~(SVf_OK|		\
918 919
						  SVf_IVisUV|SVf_UTF8),	\
				    SvFLAGS(sv) |= (SVf_POK|SVp_POK))
920 921
#define SvPOK_only_UTF8(sv)	(assert_not_ROK(sv) assert_not_glob(sv)	\
				 SvFLAGS(sv) &= ~(SVf_OK|		\
922 923 924
						  SVf_IVisUV),		\
				    SvFLAGS(sv) |= (SVf_POK|SVp_POK))

925 926 927 928 929 930
#define SvVOK(sv)		(SvMAGICAL(sv)				\
				 && mg_find(sv,PERL_MAGIC_vstring))
/* returns the vstring magic, if any */
#define SvVSTRING_mg(sv)	(SvMAGICAL(sv) \
				 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)

931
#define SvOOK(sv)		(SvFLAGS(sv) & SVf_OOK)
932
#define SvOOK_on(sv)		(SvFLAGS(sv) |= SVf_OOK)
933
#define SvOOK_off(sv)		((void)(SvOOK(sv) && sv_backoff(sv)))
934 935 936 937 938 939 940

#define SvFAKE(sv)		(SvFLAGS(sv) & SVf_FAKE)
#define SvFAKE_on(sv)		(SvFLAGS(sv) |= SVf_FAKE)
#define SvFAKE_off(sv)		(SvFLAGS(sv) &= ~SVf_FAKE)

#define SvROK(sv)		(SvFLAGS(sv) & SVf_ROK)
#define SvROK_on(sv)		(SvFLAGS(sv) |= SVf_ROK)
941
#define SvROK_off(sv)		(SvFLAGS(sv) &= ~(SVf_ROK))
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

#define SvMAGICAL(sv)		(SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
#define SvMAGICAL_on(sv)	(SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
#define SvMAGICAL_off(sv)	(SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))

#define SvGMAGICAL(sv)		(SvFLAGS(sv) & SVs_GMG)
#define SvGMAGICAL_on(sv)	(SvFLAGS(sv) |= SVs_GMG)
#define SvGMAGICAL_off(sv)	(SvFLAGS(sv) &= ~SVs_GMG)

#define SvSMAGICAL(sv)		(SvFLAGS(sv) & SVs_SMG)
#define SvSMAGICAL_on(sv)	(SvFLAGS(sv) |= SVs_SMG)
#define SvSMAGICAL_off(sv)	(SvFLAGS(sv) &= ~SVs_SMG)

#define SvRMAGICAL(sv)		(SvFLAGS(sv) & SVs_RMG)
#define SvRMAGICAL_on(sv)	(SvFLAGS(sv) |= SVs_RMG)
#define SvRMAGICAL_off(sv)	(SvFLAGS(sv) &= ~SVs_RMG)

959 960 961 962 963 964 965 966 967
#define SvAMAGIC(sv)		(SvROK(sv) && SvOBJECT(SvRV(sv)) &&	\
				 HvAMAGIC(SvSTASH(SvRV(sv))))

/* To be used on the stashes themselves: */
#define HvAMAGIC(hv)		(SvFLAGS(hv) & SVf_AMAGIC)
#define HvAMAGIC_on(hv)		(SvFLAGS(hv) |= SVf_AMAGIC)
#define HvAMAGIC_off(hv)	(SvFLAGS(hv) &=~ SVf_AMAGIC)


968
/* "nog" means "doesn't have get magic" */
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
#define SvPOK_nog(sv)		((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
#define SvIOK_nog(sv)		((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
#define SvUOK_nog(sv)		((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
#define SvNOK_nog(sv)		((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
#define SvNIOK_nog(sv)		(SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))

#define SvPOK_nogthink(sv)	((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
#define SvIOK_nogthink(sv)	((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
#define SvUOK_nogthink(sv)	((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
#define SvNOK_nogthink(sv)	((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
#define SvNIOK_nogthink(sv)	(SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))

#define SvPOK_utf8_nog(sv)	((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
#define SvPOK_utf8_nogthink(sv)	((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))

#define SvPOK_byte_nog(sv)	((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
#define SvPOK_byte_nogthink(sv)	((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)

#define SvPOK_pure_nogthink(sv) \
    ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
#define SvPOK_utf8_pure_nogthink(sv) \
    ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
#define SvPOK_byte_pure_nogthink(sv) \
    ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
993 994

/*
995
=for apidoc Am|U32|SvGAMAGIC|SV* sv
996

997 998
Returns true if the SV has get magic or
overloading.  If either is true then
999
the scalar is active data, and has the potential to return a new value every
1000 1001 1002
time it is accessed.  Hence you must be careful to
only read it once per user logical operation and work
with that returned value.  If neither is true then
1003 1004 1005
the scalar's value cannot change unless written to.

=cut
1006
*/
1007 1008 1009

#define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))

1010 1011 1012 1013
#define Gv_AMG(stash) \
	(HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
	    ? 1					    \
	    : (HvAMAGIC_off(stash), 0))
1014 1015 1016 1017 1018 1019

#define SvWEAKREF(sv)		((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
				  == (SVf_ROK|SVprv_WEAKREF))
#define SvWEAKREF_on(sv)	(SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
#define SvWEAKREF_off(sv)	(SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))

1020 1021 1022 1023 1024
#define SvPCS_IMPORTED(sv)	((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
				 == (SVf_ROK|SVprv_PCS_IMPORTED))
#define SvPCS_IMPORTED_on(sv)	(SvFLAGS(sv) |=  (SVf_ROK|SVprv_PCS_IMPORTED))
#define SvPCS_IMPORTED_off(sv)	(SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
/*
=for apidoc m|U32|SvTHINKFIRST|SV *sv

A quick flag check to see whether an sv should be passed to sv_force_normal
to be "downgraded" before SvIVX or SvPVX can be modified directly.

For example, if your scalar is a reference and you want to modify the SvIVX
slot, you can't just do SvROK_off, as that will leak the referent.

This is used internally by various sv-modifying functions, such as
sv_setsv, sv_setiv and sv_pvn_force.

One case that this does not handle is a gv without SvFAKE set.  After

    if (SvTHINKFIRST(gv)) sv_force_normal(gv);

it will still be a gv.

SvTHINKFIRST sometimes produces false positives.  In those cases
sv_force_normal does nothing.

=cut
*/

1049 1050 1051
#define SvTHINKFIRST(sv)	(SvFLAGS(sv) & SVf_THINKFIRST)

#define SvPADMY(sv)		(SvFLAGS(sv) & SVs_PADMY)
1052
#define SvPADMY_on(sv)		(SvFLAGS(sv) |= SVs_PADMY)
1053

1054 1055 1056 1057 1058 1059
/* SVs_PADTMP and SVs_PADSTALE share the same bit, mediated by SVs_PADMY */

#define SvPADTMP(sv)	((SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP)) == SVs_PADTMP)
#define SvPADSTALE(sv)	((SvFLAGS(sv) & (SVs_PADMY|SVs_PADSTALE)) \
				    == (SVs_PADMY|SVs_PADSTALE))

1060 1061 1062 1063
#define SvPADTMP_on(sv)		S_SvPADTMP_on(MUTABLE_SV(sv))
#define SvPADTMP_off(sv)	S_SvPADTMP_off(MUTABLE_SV(sv))
#define SvPADSTALE_on(sv)	S_SvPADSTALE_on(MUTABLE_SV(sv))
#define SvPADSTALE_off(sv)	S_SvPADSTALE_off(MUTABLE_SV(sv))
1064

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
#define SvTEMP(sv)		(SvFLAGS(sv) & SVs_TEMP)
#define SvTEMP_on(sv)		(SvFLAGS(sv) |= SVs_TEMP)
#define SvTEMP_off(sv)		(SvFLAGS(sv) &= ~SVs_TEMP)

#define SvOBJECT(sv)		(SvFLAGS(sv) & SVs_OBJECT)
#define SvOBJECT_on(sv)		(SvFLAGS(sv) |= SVs_OBJECT)
#define SvOBJECT_off(sv)	(SvFLAGS(sv) &= ~SVs_OBJECT)

#define SvREADONLY(sv)		(SvFLAGS(sv) & SVf_READONLY)
#define SvREADONLY_on(sv)	(SvFLAGS(sv) |= SVf_READONLY)
#define SvREADONLY_off(sv)	(SvFLAGS(sv) &= ~SVf_READONLY)

1077
#define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1078 1079 1080
#define SvSCREAM_on(sv)		(SvFLAGS(sv) |= SVp_SCREAM)
#define SvSCREAM_off(sv)	(SvFLAGS(sv) &= ~SVp_SCREAM)

1081 1082 1083 1084 1085
#ifndef PERL_CORE
#  define SvCOMPILED(sv)	0
#  define SvCOMPILED_on(sv)
#  define SvCOMPILED_off(sv)
#endif
1086 1087 1088 1089 1090

#define SvEVALED(sv)		(SvFLAGS(sv) & SVrepl_EVAL)
#define SvEVALED_on(sv)		(SvFLAGS(sv) |= SVrepl_EVAL)
#define SvEVALED_off(sv)	(SvFLAGS(sv) &= ~SVrepl_EVAL)

1091
#if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1092
#  define SvVALID(sv)		({ const SV *const _svvalid = (const SV*)(sv); \
1093
				   if (SvFLAGS(_svvalid) & SVpbm_VALID && !SvSCREAM(_svvalid)) \
1094 1095
				       assert(!isGV_with_GP(_svvalid));	\
				   (SvFLAGS(_svvalid) & SVpbm_VALID);	\
1096
				})
1097 1098
#  define SvVALID_on(sv)	({ SV *const _svvalid = MUTABLE_SV(sv);	\
				   assert(!isGV_with_GP(_svvalid));	\
1099
				   assert(!SvSCREAM(_svvalid));		\
1100
				   (SvFLAGS(_svvalid) |= SVpbm_VALID);	\
1101
				})
1102 1103
#  define SvVALID_off(sv)	({ SV *const _svvalid = MUTABLE_SV(sv);	\
				   assert(!isGV_with_GP(_svvalid));	\
1104
				   assert(!SvSCREAM(_svvalid));		\
1105
				   (SvFLAGS(_svvalid) &= ~SVpbm_VALID);	\
1106 1107
				})

1108
#  define SvTAIL(sv)	({ const SV *const _svtail = (const SV *)(sv);	\
1109 1110 1111
			    assert(SvTYPE(_svtail) != SVt_PVAV);	\
			    assert(SvTYPE(_svtail) != SVt_PVHV);	\
			    assert(!SvSCREAM(_svtail));			\
1112 1113 1114 1115
			    (SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))	\
				== (SVpbm_TAIL|SVpbm_VALID);		\
			})
#else
1116
#  define SvVALID(sv)		((SvFLAGS(sv) & SVpbm_VALID) && !SvSCREAM(sv))
1117 1118 1119 1120 1121 1122
#  define SvVALID_on(sv)	(SvFLAGS(sv) |= SVpbm_VALID)
#  define SvVALID_off(sv)	(SvFLAGS(sv) &= ~SVpbm_VALID)
#  define SvTAIL(sv)	    ((SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))	\
			     == (SVpbm_TAIL|SVpbm_VALID))

#endif
1123 1124 1125 1126
#define SvTAIL_on(sv)		(SvFLAGS(sv) |= SVpbm_TAIL)
#define SvTAIL_off(sv)		(SvFLAGS(sv) &= ~SVpbm_TAIL)


1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
#define SvPAD_TYPED(sv) \
	((SvFLAGS(sv) & (SVpad_NAME|SVpad_TYPED)) == (SVpad_NAME|SVpad_TYPED))

#define SvPAD_OUR(sv)	\
	((SvFLAGS(sv) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))

#define SvPAD_STATE(sv)	\
	((SvFLAGS(sv) & (SVpad_NAME|SVpad_STATE)) == (SVpad_NAME|SVpad_STATE))

#if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
#  define SvPAD_TYPED_on(sv)	({					\
1138 1139 1140
	    SV *const _svpad = MUTABLE_SV(sv);				\
	    assert(SvTYPE(_svpad) == SVt_PVMG);				\
	    (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_TYPED);		\
1141 1142
	})
#define SvPAD_OUR_on(sv)	({					\
1143 1144 1145
	    SV *const _svpad = MUTABLE_SV(sv);				\
	    assert(SvTYPE(_svpad) == SVt_PVMG);				\
	    (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_OUR);			\
1146 1147
	})
#define SvPAD_STATE_on(sv)	({					\
1148 1149 1150
	    SV *const _svpad = MUTABLE_SV(sv);				\
	    assert(SvTYPE(_svpad) == SVt_PVNV || SvTYPE(_svpad) == SVt_PVMG); \
	    (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_STATE);		\
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	})
#else
#  define SvPAD_TYPED_on(sv)	(SvFLAGS(sv) |= SVpad_NAME|SVpad_TYPED)
#  define SvPAD_OUR_on(sv)	(SvFLAGS(sv) |= SVpad_NAME|SVpad_OUR)
#  define SvPAD_STATE_on(sv)	(SvFLAGS(sv) |= SVpad_NAME|SVpad_STATE)
#endif

#define SvOURSTASH(sv)	\
	(SvPAD_OUR(sv) ? ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash : NULL)
#define SvOURSTASH_set(sv, st)					\
        STMT_START {						\
	    assert(SvTYPE(sv) == SVt_PVMG);			\
	    ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash = st;	\
	} STMT_END

1166 1167
#define SvRVx(sv) SvRV(sv)

1168 1169 1170 1171 1172 1173 1174
#ifdef PERL_DEBUG_COW
/* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
   +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
#  define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
#  define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
#  define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
#  define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1175
#  define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1176 1177
/* Don't test the core XS code yet.  */
#  if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1178
#    define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1179 1180 1181 1182 1183 1184 1185
#  else
#  define SvPVX(sv) SvPVX_mutable(sv)
#  endif
#  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
#  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
#  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)

1186 1187
#  define SvMAGIC(sv)	(0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic))
#  define SvSTASH(sv)	(0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_stash))
1188 1189 1190 1191 1192 1193 1194
#else
#  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
#  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)

#  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
/* These get expanded inside other macros that already use a variable _sv  */
#    define SvPVX(sv)							\
1195
	(*({ SV *const _svpvx = MUTABLE_SV(sv);				\
1196
	    assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]);	\
1197
	    assert(!isGV_with_GP(_svpvx));				\
1198 1199
	    assert(!(SvTYPE(_svpvx) == SVt_PVIO				\
		     && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP)));		\
1200
	    &((_svpvx)->sv_u.svu_pv);					\
1201 1202
	 }))
#    define SvCUR(sv)							\
1203
	(*({ const SV *const _svcur = (const SV *)(sv);			\
1204 1205
	    assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]	\
		|| SvTYPE(_svcur) == SVt_REGEXP);			\
1206
	    assert(!isGV_with_GP(_svcur));				\
1207 1208
	    assert(!(SvTYPE(_svcur) == SVt_PVIO				\
		     && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));		\
1209
	    &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);		\
1210 1211
	 }))
#    define SvIVX(sv)							\
1212
	(*({ const SV *const _svivx = (const SV *)(sv);			\
1213
	    assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]);	\
1214 1215
	    assert(!isGV_with_GP(_svivx));				\
	    &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv);		\
1216 1217
	 }))
#    define SvUVX(sv)							\
1218
	(*({ const SV *const _svuvx = (const SV *)(sv);			\
1219
	    assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]);	\
1220 1221
	    assert(!isGV_with_GP(_svuvx));				\
	    &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv);		\
1222 1223
	 }))
#    define SvNVX(sv)							\
1224
	(*({ const SV *const _svnvx = (const SV *)(sv);			\
1225
	    assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]);	\
1226 1227
	    assert(!isGV_with_GP(_svnvx));				\
	    &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv);	\
1228 1229
	 }))
#    define SvRV(sv)							\
1230
	(*({ SV *const _svrv = MUTABLE_SV(sv);				\
1231
	    assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);	\
1232
	    assert(!isGV_with_GP(_svrv));				\
1233 1234
	    assert(!(SvTYPE(_svrv) == SVt_PVIO				\
		     && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));		\
1235
	    &((_svrv)->sv_u.svu_rv);					\
1236
	 }))
1237 1238
#    define SvRV_const(sv)						\
	({ const SV *const _svrv = (const SV *)(sv);			\
1239
	    assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);	\
1240
	    assert(!isGV_with_GP(_svrv));				\
1241 1242
	    assert(!(SvTYPE(_svrv) == SVt_PVIO				\
		     && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));		\
1243 1244
	    (_svrv)->sv_u.svu_rv;					\
	 })
1245
#    define SvMAGIC(sv)							\
1246 1247 1248 1249 1250
	(*({ const SV *const _svmagic = (const SV *)(sv);		\
	    assert(SvTYPE(_svmagic) >= SVt_PVMG);			\
	    if(SvTYPE(_svmagic) == SVt_PVMG)				\
		assert(!SvPAD_OUR(_svmagic));				\
	    &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1251 1252
	  }))
#    define SvSTASH(sv)							\
1253 1254 1255
	(*({ const SV *const _svstash = (const SV *)(sv);		\
	    assert(SvTYPE(_svstash) >= SVt_PVMG);			\
	    &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash);	\
1256 1257 1258 1259 1260 1261 1262 1263
	  }))
#  else
#    define SvPVX(sv) ((sv)->sv_u.svu_pv)
#    define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
#    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
#    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
#    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
#    define SvRV(sv) ((sv)->sv_u.svu_rv)
1264
#    define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1265 1266 1267 1268 1269 1270
#    define SvMAGIC(sv)	((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic
#    define SvSTASH(sv)	((XPVMG*)  SvANY(sv))->xmg_stash
#  endif
#endif

#ifndef PERL_POISON
1271 1272
/* Given that these two are new, there can't be any existing code using them
 *  as LVALUEs  */
1273 1274 1275 1276 1277 1278 1279 1280
#  define SvPVX_mutable(sv)	(0 + (sv)->sv_u.svu_pv)
#  define SvPVX_const(sv)	((const char*)(0 + (sv)->sv_u.svu_pv))
#else
/* Except for the poison code, which uses & to scribble over the pointer after
   free() is called.  */
#  define SvPVX_mutable(sv)	((sv)->sv_u.svu_pv)
#  define SvPVX_const(sv)	((const char*)((sv)->sv_u.svu_pv))
#endif
1281 1282 1283 1284 1285 1286

#define SvIVXx(sv) SvIVX(sv)
#define SvUVXx(sv) SvUVX(sv)
#define SvNVXx(sv) SvNVX(sv)
#define SvPVXx(sv) SvPVX(sv)
#define SvLENx(sv) SvLEN(sv)
1287
#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1288

1289

1290 1291 1292 1293 1294 1295
/* Ask a scalar nicely to try to become an IV, if possible.
   Not guaranteed to stay returning void */
/* Macro won't actually call sv_2iv if already IOK */
#define SvIV_please(sv) \
	STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
		(void) SvIV(sv); } STMT_END
1296
#define SvIV_please_nomg(sv) \
1297 1298 1299
	(!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv)) \
	    ? (SvIV_nomg(sv), SvIOK(sv))	  \
	    : SvIOK(sv))
1300
#define SvIV_set(sv, val) \
1301 1302
	STMT_START { \
		assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);	\
1303
		assert(!isGV_with_GP(sv));		\
1304
		(((XPVIV*)  SvANY(sv))->xiv_iv = (val)); } STMT_END
1305
#define SvNV_set(sv, val) \
1306 1307
	STMT_START { \
		assert(PL_valid_types_NV_set[SvTYPE(sv) & SVt_MASK]);	\
1308 1309
		assert(!isGV_with_GP(sv));		\
		(((XPVNV*)SvANY(sv))->xnv_u.xnv_nv = (val)); } STMT_END
1310
#define SvPV_set(sv, val) \
1311 1312
	STMT_START { \
		assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);	\
1313
		assert(!isGV_with_GP(sv));		\
1314 1315
		assert(!(SvTYPE(sv) == SVt_PVIO		\
		     && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1316
		((sv)->sv_u.svu_pv = (val)); } STMT_END
1317
#define SvUV_set(sv, val) \
1318 1319
	STMT_START { \
		assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);	\
1320
		assert(!isGV_with_GP(sv));		\
1321 1322
		(((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
#define SvRV_set(sv, val) \
1323 1324
        STMT_START { \
		assert(PL_valid_types_RV[SvTYPE(sv) & SVt_MASK]);	\
1325
		assert(!isGV_with_GP(sv));		\
1326 1327
		assert(!(SvTYPE(sv) == SVt_PVIO		\
		     && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1328
                ((sv)->sv_u.svu_rv = (val)); } STMT_END
1329
#define SvMAGIC_set(sv, val) \
1330 1331
        STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
                (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1332
#define SvSTASH_set(sv, val) \
1333
        STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1334
                (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
1335
#define SvCUR_set(sv, val) \
1336
	STMT_START { \
1337 1338
		assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]	\
			|| SvTYPE(sv) == SVt_REGEXP);	\
1339
		assert(!isGV_with_GP(sv));		\
1340 1341
		assert(!(SvTYPE(sv) == SVt_PVIO		\
		     && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1342
		(((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
1343
#define SvLEN_set(sv, val) \
1344 1345
	STMT_START { \
		assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);	\
1346
		assert(!isGV_with_GP(sv));	\
1347 1348
		assert(!(SvTYPE(sv) == SVt_PVIO		\
		     && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1349
		(((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
1350 1351
#define SvEND_set(sv, val) \
	STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1352
		SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1353

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
#define SvPV_renew(sv,n) \
	STMT_START { SvLEN_set(sv, n); \
		SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)			\
				(char*)saferealloc((Malloc_t)SvPVX(sv), \
						   (MEM_SIZE)((n)))));  \
		 } STMT_END

#define SvPV_shrink_to_cur(sv) STMT_START { \
		   const STRLEN _lEnGtH = SvCUR(sv) + 1; \
		   SvPV_renew(sv, _lEnGtH); \
		 } STMT_END

1366 1367 1368 1369
#define SvPV_free(sv)							\
    STMT_START {							\
		     assert(SvTYPE(sv) >= SVt_PV);			\
		     if (SvLEN(sv)) {					\
1370
			 assert(!SvROK(sv));				\
1371
			 if(UNLIKELY(SvOOK(sv))) {			\
1372 1373 1374
			     STRLEN zok; 				\
			     SvOOK_offset(sv, zok);			\
			     SvPV_set(sv, SvPVX_mutable(sv) - zok);	\
1375 1376 1377 1378 1379
			     SvFLAGS(sv) &= ~SVf_OOK;			\
			 }						\
			 Safefree(SvPVX(sv));				\
		     }							\
		 } STMT_END
1380

1381 1382 1383 1384 1385
#ifdef PERL_CORE
/* Code that crops up in three places to take a scalar and ready it to hold
   a reference */
#  define prepare_SV_for_RV(sv)						\
    STMT_START {							\
1386 1387 1388
		    if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)	\
			sv_upgrade(sv, SVt_IV);				\
		    else if (SvTYPE(sv) >= SVt_PV) {			\
1389 1390 1391 1392 1393 1394
			SvPV_free(sv);					\
			SvLEN_set(sv, 0);				\
                        SvCUR_set(sv, 0);				\
		    }							\
		 } STMT_END
#endif
1395

1396 1397 1398
#ifndef PERL_CORE
#  define BmFLAGS(sv)		(SvTAIL(sv) ? FBMcf_TAIL : 0)
#endif
1399 1400 1401

#if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
#  define BmUSEFUL(sv)							\
1402
	(*({ SV *const _bmuseful = MUTABLE_SV(sv);			\
1403
	    assert(SvTYPE(_bmuseful) >= SVt_PVIV);			\
1404 1405
	    assert(SvVALID(_bmuseful));					\
	    assert(!SvIOK(_bmuseful));					\
1406
	    &(((XPVIV*) SvANY(_bmuseful))->xiv_u.xivu_iv);              \
1407 1408
	 }))
#else
1409
#  define BmUSEFUL(sv)          ((XPVIV*) SvANY(sv))->xiv_u.xivu_iv
1410 1411

#endif
1412

1413 1414 1415 1416 1417
#ifndef PERL_CORE
# define BmRARE(sv)	0
# define BmPREVIOUS(sv)	0
#endif

1418
#define FmLINES(sv)	((XPVIV*)  SvANY(sv))->xiv_iv
1419 1420 1421 1422

#define LvTYPE(sv)	((XPVLV*)  SvANY(sv))->xlv_type
#define LvTARG(sv)	((XPVLV*)  SvANY(sv))->xlv_targ
#define LvTARGOFF(sv)	((XPVLV*)  SvANY(sv))->xlv_targoff
1423
#define LvSTARGOFF(sv)	((XPVLV*)  SvANY(sv))->xlv_targoff_u.xlvu_stargoff
1424
#define LvTARGLEN(sv)	((XPVLV*)  SvANY(sv))->xlv_targlen
1425
#define LvFLAGS(sv)	((XPVLV*)  SvANY(sv))->xlv_flags
1426

1427
#define IoIFP(sv)	(sv)->sv_u.svu_fp
1428 1429 1430
#define IoOFP(sv)	((XPVIO*)  SvANY(sv))->xio_ofp
#define IoDIRP(sv)	((XPVIO*)  SvANY(sv))->xio_dirp
#define IoANY(sv)	((XPVIO*)  SvANY(sv))->xio_any
1431
#define IoLINES(sv)	((XPVIO*)  SvANY(sv))->xiv_u.xivu_iv
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
#define IoPAGE(sv)	((XPVIO*)  SvANY(sv))->xio_page
#define IoPAGE_LEN(sv)	((XPVIO*)  SvANY(sv))->xio_page_len
#define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
#define IoTOP_NAME(sv)	((XPVIO*)  SvANY(sv))->xio_top_name
#define IoTOP_GV(sv)	((XPVIO*)  SvANY(sv))->xio_top_gv
#define IoFMT_NAME(sv)	((XPVIO*)  SvANY(sv))->xio_fmt_name
#define IoFMT_GV(sv)	((XPVIO*)  SvANY(sv))->xio_fmt_gv
#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
#define IoBOTTOM_GV(sv)	((XPVIO*)  SvANY(sv))->xio_bottom_gv
#define IoTYPE(sv)	((XPVIO*)  SvANY(sv))->xio_type
#define IoFLAGS(sv)	((XPVIO*)  SvANY(sv))->xio_flags

/* IoTYPE(sv) is a single character telling the type of I/O connection. */
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
#define IoTYPE_RDONLY		'<'
#define IoTYPE_WRONLY		'>'
#define IoTYPE_RDWR		'+'
#define IoTYPE_APPEND 		'a'
#define IoTYPE_PIPE		'|'
#define IoTYPE_STD		'-'	/* stdin or stdout */
#define IoTYPE_SOCKET		's'
#define IoTYPE_CLOSED		' '
#define IoTYPE_IMPLICIT		'I'	/* stdin or stdout or stderr */
#define IoTYPE_NUMERIC		'#'	/* fdopen */
1455 1456 1457

/*
=for apidoc Am|bool|SvTAINTED|SV* sv
1458
Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
1459 1460 1461
not.

=for apidoc Am|void|SvTAINTED_on|SV* sv
1462
Marks an SV as tainted if tainting is enabled.
1463 1464

=for apidoc Am|void|SvTAINTED_off|SV* sv
1465 1466
Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
some of Perl's fundamental security features.  XS module authors should not
1467
use this function unless they fully understand all the implications of
1468
unconditionally untainting the value.  Untainting should be done in the
1469 1470 1471 1472
standard perl fashion, via a carefully crafted regexp, rather than directly
untainting variables.

=for apidoc Am|void|SvTAINT|SV* sv
1473 1474 1475 1476 1477
Taints an SV if tainting is enabled, and if some input to the current
expression is tainted--usually a variable, but possibly also implicit
inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
the outputs of an expression in a pessimistic fashion; i.e., without paying
attention to precisely which outputs are influenced by which inputs.
1478 1479 1480 1481

=cut
*/

1482 1483
#define sv_taint(sv)	  sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)

1484
#ifdef NO_TAINT_SUPPORT
1485 1486 1487 1488
#   define SvTAINTED(sv) 0
#else
#   define SvTAINTED(sv)	  (SvMAGICAL(sv) && sv_tainted(sv))
#endif
1489 1490
#define SvTAINTED_on(sv)  STMT_START{ if(UNLIKELY(TAINTING_get)){sv_taint(sv);}   }STMT_END
#define SvTAINTED_off(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_untaint(sv);} }STMT_END
1491 1492 1493

#define SvTAINT(sv)			\
    STMT_START {			\
1494 1495
	if (UNLIKELY(TAINTING_get)) {	\
	    if (UNLIKELY(TAINT_get))	\
1496 1497 1498 1499 1500 1501
		SvTAINTED_on(sv);	\
	}				\
    } STMT_END

/*
=for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1502
Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1503
only a string (C<SvPOK_only>), by hook or by crook.  You need force if you are
1504
going to update the C<SvPVX> directly.  Processes get magic.
1505

1506
Note that coercing an arbitrary scalar into a plain PV will potentially
1507
strip useful data from it.  For example if the SV was C<SvROK>, then the
1508 1509 1510 1511
referent will have its reference count decremented, and the SV itself may
be converted to an C<SvPOK> scalar with a string buffer containing a value
such as C<"ARRAY(0x1234)">.

1512
=for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1513
Like C<SvPV_force>, but doesn't process get magic.
1514 1515

=for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1516 1517
Returns a pointer to the string in the SV, or a stringified form of
the SV if the SV does not contain a string.  The SV may cache the
1518 1519
stringified version becoming C<SvPOK>.  Handles 'get' magic.  The
C<len> variable will be set to the length of the string (this is a macro, so
1520
don't use C<&len>).  See also C<SvPVx> for a version which guarantees to
1521
evaluate sv only once.
1522

1523 1524
Note that there is no guarantee that the return value of C<SvPV()> is
equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or that
1525 1526
successive calls to C<SvPV(sv)> will return the same pointer value each
time.  This is due to the way that things like overloading and
1527 1528 1529 1530 1531
Copy-On-Write are handled.  In these cases, the return value may point to
a temporary buffer or similar.  If you absolutely need the SvPVX field to
be valid (for example, if you intend to write to it), then see
L</SvPV_force>.

1532
=for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1533 1534
A version of C<SvPV> which guarantees to evaluate C<sv> only once.
Only use this if C<sv> is an expression with side effects, otherwise use the
1535
more efficient C<SvPV>.
1536 1537 1538

=for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
Like C<SvPV> but doesn't process magic.