| 1 | /* op.c |
| 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 | /* |
| 11 | * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was |
| 12 | * our Mr. Bilbo's first cousin on the mother's side (her mother being the |
| 13 | * youngest of the Old Took's daughters); and Mr. Drogo was his second |
| 14 | * cousin. So Mr. Frodo is his first *and* second cousin, once removed |
| 15 | * either way, as the saying is, if you follow me." --the Gaffer |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include "EXTERN.h" |
| 20 | #define PERL_IN_OP_C |
| 21 | #include "perl.h" |
| 22 | #include "keywords.h" |
| 23 | |
| 24 | #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o) |
| 25 | |
| 26 | #if defined(PL_OP_SLAB_ALLOC) |
| 27 | |
| 28 | #ifndef PERL_SLAB_SIZE |
| 29 | #define PERL_SLAB_SIZE 2048 |
| 30 | #endif |
| 31 | |
| 32 | #define NewOp(m,var,c,type) \ |
| 33 | STMT_START { var = (type *) Slab_Alloc(m,c*sizeof(type)); } STMT_END |
| 34 | |
| 35 | #define FreeOp(p) Slab_Free(p) |
| 36 | |
| 37 | STATIC void * |
| 38 | S_Slab_Alloc(pTHX_ int m, size_t sz) |
| 39 | { |
| 40 | /* |
| 41 | * To make incrementing use count easy PL_OpSlab is an I32 * |
| 42 | * To make inserting the link to slab PL_OpPtr is I32 ** |
| 43 | * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments |
| 44 | * Add an overhead for pointer to slab and round up as a number of pointers |
| 45 | */ |
| 46 | sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *); |
| 47 | if ((PL_OpSpace -= sz) < 0) { |
| 48 | PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*)); |
| 49 | if (!PL_OpPtr) { |
| 50 | return NULL; |
| 51 | } |
| 52 | Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **); |
| 53 | /* We reserve the 0'th I32 sized chunk as a use count */ |
| 54 | PL_OpSlab = (I32 *) PL_OpPtr; |
| 55 | /* Reduce size by the use count word, and by the size we need. |
| 56 | * Latter is to mimic the '-=' in the if() above |
| 57 | */ |
| 58 | PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz; |
| 59 | /* Allocation pointer starts at the top. |
| 60 | Theory: because we build leaves before trunk allocating at end |
| 61 | means that at run time access is cache friendly upward |
| 62 | */ |
| 63 | PL_OpPtr += PERL_SLAB_SIZE; |
| 64 | } |
| 65 | assert( PL_OpSpace >= 0 ); |
| 66 | /* Move the allocation pointer down */ |
| 67 | PL_OpPtr -= sz; |
| 68 | assert( PL_OpPtr > (I32 **) PL_OpSlab ); |
| 69 | *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */ |
| 70 | (*PL_OpSlab)++; /* Increment use count of slab */ |
| 71 | assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) ); |
| 72 | assert( *PL_OpSlab > 0 ); |
| 73 | return (void *)(PL_OpPtr + 1); |
| 74 | } |
| 75 | |
| 76 | STATIC void |
| 77 | S_Slab_Free(pTHX_ void *op) |
| 78 | { |
| 79 | I32 **ptr = (I32 **) op; |
| 80 | I32 *slab = ptr[-1]; |
| 81 | assert( ptr-1 > (I32 **) slab ); |
| 82 | assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) ); |
| 83 | assert( *slab > 0 ); |
| 84 | if (--(*slab) == 0) { |
| 85 | #ifdef NETWARE |
| 86 | #define PerlMemShared PerlMem |
| 87 | #endif |
| 88 | |
| 89 | PerlMemShared_free(slab); |
| 90 | if (slab == PL_OpSlab) { |
| 91 | PL_OpSpace = 0; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #else |
| 97 | #define NewOp(m, var, c, type) Newz(m, var, c, type) |
| 98 | #define FreeOp(p) Safefree(p) |
| 99 | #endif |
| 100 | /* |
| 101 | * In the following definition, the ", Nullop" is just to make the compiler |
| 102 | * think the expression is of the right type: croak actually does a Siglongjmp. |
| 103 | */ |
| 104 | #define CHECKOP(type,o) \ |
| 105 | ((PL_op_mask && PL_op_mask[type]) \ |
| 106 | ? ( op_free((OP*)o), \ |
| 107 | Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \ |
| 108 | Nullop ) \ |
| 109 | : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o)) |
| 110 | |
| 111 | #define PAD_MAX 999999999 |
| 112 | #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2) |
| 113 | |
| 114 | STATIC char* |
| 115 | S_gv_ename(pTHX_ GV *gv) |
| 116 | { |
| 117 | STRLEN n_a; |
| 118 | SV* tmpsv = sv_newmortal(); |
| 119 | gv_efullname3(tmpsv, gv, Nullch); |
| 120 | return SvPV(tmpsv,n_a); |
| 121 | } |
| 122 | |
| 123 | STATIC OP * |
| 124 | S_no_fh_allowed(pTHX_ OP *o) |
| 125 | { |
| 126 | yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function", |
| 127 | OP_DESC(o))); |
| 128 | return o; |
| 129 | } |
| 130 | |
| 131 | STATIC OP * |
| 132 | S_too_few_arguments(pTHX_ OP *o, char *name) |
| 133 | { |
| 134 | yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name)); |
| 135 | return o; |
| 136 | } |
| 137 | |
| 138 | STATIC OP * |
| 139 | S_too_many_arguments(pTHX_ OP *o, char *name) |
| 140 | { |
| 141 | yyerror(Perl_form(aTHX_ "Too many arguments for %s", name)); |
| 142 | return o; |
| 143 | } |
| 144 | |
| 145 | STATIC void |
| 146 | S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid) |
| 147 | { |
| 148 | yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)", |
| 149 | (int)n, name, t, OP_DESC(kid))); |
| 150 | } |
| 151 | |
| 152 | STATIC void |
| 153 | S_no_bareword_allowed(pTHX_ OP *o) |
| 154 | { |
| 155 | qerror(Perl_mess(aTHX_ |
| 156 | "Bareword \"%s\" not allowed while \"strict subs\" in use", |
| 157 | SvPV_nolen(cSVOPo_sv))); |
| 158 | } |
| 159 | |
| 160 | /* "register" allocation */ |
| 161 | |
| 162 | PADOFFSET |
| 163 | Perl_pad_allocmy(pTHX_ char *name) |
| 164 | { |
| 165 | PADOFFSET off; |
| 166 | SV *sv; |
| 167 | |
| 168 | if (!(PL_in_my == KEY_our || |
| 169 | isALPHA(name[1]) || |
| 170 | (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) || |
| 171 | (name[1] == '_' && (int)strlen(name) > 2))) |
| 172 | { |
| 173 | if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) { |
| 174 | /* 1999-02-27 mjd@plover.com */ |
| 175 | char *p; |
| 176 | p = strchr(name, '\0'); |
| 177 | /* The next block assumes the buffer is at least 205 chars |
| 178 | long. At present, it's always at least 256 chars. */ |
| 179 | if (p-name > 200) { |
| 180 | strcpy(name+200, "..."); |
| 181 | p = name+199; |
| 182 | } |
| 183 | else { |
| 184 | p[1] = '\0'; |
| 185 | } |
| 186 | /* Move everything else down one character */ |
| 187 | for (; p-name > 2; p--) |
| 188 | *p = *(p-1); |
| 189 | name[2] = toCTRL(name[1]); |
| 190 | name[1] = '^'; |
| 191 | } |
| 192 | yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name)); |
| 193 | } |
| 194 | if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) { |
| 195 | SV **svp = AvARRAY(PL_comppad_name); |
| 196 | HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash); |
| 197 | PADOFFSET top = AvFILLp(PL_comppad_name); |
| 198 | for (off = top; (I32)off > PL_comppad_name_floor; off--) { |
| 199 | if ((sv = svp[off]) |
| 200 | && sv != &PL_sv_undef |
| 201 | && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0) |
| 202 | && (PL_in_my != KEY_our |
| 203 | || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)) |
| 204 | && strEQ(name, SvPVX(sv))) |
| 205 | { |
| 206 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 207 | "\"%s\" variable %s masks earlier declaration in same %s", |
| 208 | (PL_in_my == KEY_our ? "our" : "my"), |
| 209 | name, |
| 210 | (SvIVX(sv) == PAD_MAX ? "scope" : "statement")); |
| 211 | --off; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | if (PL_in_my == KEY_our) { |
| 216 | do { |
| 217 | if ((sv = svp[off]) |
| 218 | && sv != &PL_sv_undef |
| 219 | && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0) |
| 220 | && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash) |
| 221 | && strEQ(name, SvPVX(sv))) |
| 222 | { |
| 223 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 224 | "\"our\" variable %s redeclared", name); |
| 225 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 226 | "\t(Did you mean \"local\" instead of \"our\"?)\n"); |
| 227 | break; |
| 228 | } |
| 229 | } while ( off-- > 0 ); |
| 230 | } |
| 231 | } |
| 232 | off = pad_alloc(OP_PADSV, SVs_PADMY); |
| 233 | sv = NEWSV(1102,0); |
| 234 | sv_upgrade(sv, SVt_PVNV); |
| 235 | sv_setpv(sv, name); |
| 236 | if (PL_in_my_stash) { |
| 237 | if (*name != '$') |
| 238 | yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"", |
| 239 | name, PL_in_my == KEY_our ? "our" : "my")); |
| 240 | SvFLAGS(sv) |= SVpad_TYPED; |
| 241 | (void)SvUPGRADE(sv, SVt_PVMG); |
| 242 | SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash); |
| 243 | } |
| 244 | if (PL_in_my == KEY_our) { |
| 245 | (void)SvUPGRADE(sv, SVt_PVGV); |
| 246 | GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash); |
| 247 | SvFLAGS(sv) |= SVpad_OUR; |
| 248 | } |
| 249 | av_store(PL_comppad_name, off, sv); |
| 250 | SvNVX(sv) = (NV)PAD_MAX; |
| 251 | SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */ |
| 252 | if (!PL_min_intro_pending) |
| 253 | PL_min_intro_pending = off; |
| 254 | PL_max_intro_pending = off; |
| 255 | if (*name == '@') |
| 256 | av_store(PL_comppad, off, (SV*)newAV()); |
| 257 | else if (*name == '%') |
| 258 | av_store(PL_comppad, off, (SV*)newHV()); |
| 259 | SvPADMY_on(PL_curpad[off]); |
| 260 | return off; |
| 261 | } |
| 262 | |
| 263 | STATIC PADOFFSET |
| 264 | S_pad_addlex(pTHX_ SV *proto_namesv) |
| 265 | { |
| 266 | SV *namesv = NEWSV(1103,0); |
| 267 | PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY); |
| 268 | sv_upgrade(namesv, SVt_PVNV); |
| 269 | sv_setpv(namesv, SvPVX(proto_namesv)); |
| 270 | av_store(PL_comppad_name, newoff, namesv); |
| 271 | SvNVX(namesv) = (NV)PL_curcop->cop_seq; |
| 272 | SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */ |
| 273 | SvFAKE_on(namesv); /* A ref, not a real var */ |
| 274 | if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */ |
| 275 | SvFLAGS(namesv) |= SVpad_OUR; |
| 276 | (void)SvUPGRADE(namesv, SVt_PVGV); |
| 277 | GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv)); |
| 278 | } |
| 279 | if (SvFLAGS(proto_namesv) & SVpad_TYPED) { /* A typed lexical */ |
| 280 | SvFLAGS(namesv) |= SVpad_TYPED; |
| 281 | (void)SvUPGRADE(namesv, SVt_PVMG); |
| 282 | SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv)); |
| 283 | } |
| 284 | return newoff; |
| 285 | } |
| 286 | |
| 287 | #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */ |
| 288 | |
| 289 | STATIC PADOFFSET |
| 290 | S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv, |
| 291 | I32 cx_ix, I32 saweval, U32 flags) |
| 292 | { |
| 293 | CV *cv; |
| 294 | I32 off; |
| 295 | SV *sv; |
| 296 | register I32 i; |
| 297 | register PERL_CONTEXT *cx; |
| 298 | |
| 299 | for (cv = startcv; cv; cv = CvOUTSIDE(cv)) { |
| 300 | AV *curlist = CvPADLIST(cv); |
| 301 | SV **svp = av_fetch(curlist, 0, FALSE); |
| 302 | AV *curname; |
| 303 | |
| 304 | if (!svp || *svp == &PL_sv_undef) |
| 305 | continue; |
| 306 | curname = (AV*)*svp; |
| 307 | svp = AvARRAY(curname); |
| 308 | for (off = AvFILLp(curname); off > 0; off--) { |
| 309 | if ((sv = svp[off]) && |
| 310 | sv != &PL_sv_undef && |
| 311 | seq <= (U32)SvIVX(sv) && |
| 312 | seq > (U32)I_32(SvNVX(sv)) && |
| 313 | strEQ(SvPVX(sv), name)) |
| 314 | { |
| 315 | I32 depth; |
| 316 | AV *oldpad; |
| 317 | SV *oldsv; |
| 318 | |
| 319 | depth = CvDEPTH(cv); |
| 320 | if (!depth) { |
| 321 | if (newoff) { |
| 322 | if (SvFAKE(sv)) |
| 323 | continue; |
| 324 | return 0; /* don't clone from inactive stack frame */ |
| 325 | } |
| 326 | depth = 1; |
| 327 | } |
| 328 | oldpad = (AV*)AvARRAY(curlist)[depth]; |
| 329 | oldsv = *av_fetch(oldpad, off, TRUE); |
| 330 | if (!newoff) { /* Not a mere clone operation. */ |
| 331 | newoff = pad_addlex(sv); |
| 332 | if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) { |
| 333 | /* "It's closures all the way down." */ |
| 334 | CvCLONE_on(PL_compcv); |
| 335 | if (cv == startcv) { |
| 336 | if (CvANON(PL_compcv)) |
| 337 | oldsv = Nullsv; /* no need to keep ref */ |
| 338 | } |
| 339 | else { |
| 340 | CV *bcv; |
| 341 | for (bcv = startcv; |
| 342 | bcv && bcv != cv && !CvCLONE(bcv); |
| 343 | bcv = CvOUTSIDE(bcv)) |
| 344 | { |
| 345 | if (CvANON(bcv)) { |
| 346 | /* install the missing pad entry in intervening |
| 347 | * nested subs and mark them cloneable. |
| 348 | * XXX fix pad_foo() to not use globals */ |
| 349 | AV *ocomppad_name = PL_comppad_name; |
| 350 | AV *ocomppad = PL_comppad; |
| 351 | SV **ocurpad = PL_curpad; |
| 352 | AV *padlist = CvPADLIST(bcv); |
| 353 | PL_comppad_name = (AV*)AvARRAY(padlist)[0]; |
| 354 | PL_comppad = (AV*)AvARRAY(padlist)[1]; |
| 355 | PL_curpad = AvARRAY(PL_comppad); |
| 356 | pad_addlex(sv); |
| 357 | PL_comppad_name = ocomppad_name; |
| 358 | PL_comppad = ocomppad; |
| 359 | PL_curpad = ocurpad; |
| 360 | CvCLONE_on(bcv); |
| 361 | } |
| 362 | else { |
| 363 | if (ckWARN(WARN_CLOSURE) |
| 364 | && !CvUNIQUE(bcv) && !CvUNIQUE(cv)) |
| 365 | { |
| 366 | Perl_warner(aTHX_ packWARN(WARN_CLOSURE), |
| 367 | "Variable \"%s\" may be unavailable", |
| 368 | name); |
| 369 | } |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | else if (!CvUNIQUE(PL_compcv)) { |
| 376 | if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv) |
| 377 | && !(SvFLAGS(sv) & SVpad_OUR)) |
| 378 | { |
| 379 | Perl_warner(aTHX_ packWARN(WARN_CLOSURE), |
| 380 | "Variable \"%s\" will not stay shared", name); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv)); |
| 385 | return newoff; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (flags & FINDLEX_NOSEARCH) |
| 391 | return 0; |
| 392 | |
| 393 | /* Nothing in current lexical context--try eval's context, if any. |
| 394 | * This is necessary to let the perldb get at lexically scoped variables. |
| 395 | * XXX This will also probably interact badly with eval tree caching. |
| 396 | */ |
| 397 | |
| 398 | for (i = cx_ix; i >= 0; i--) { |
| 399 | cx = &cxstack[i]; |
| 400 | switch (CxTYPE(cx)) { |
| 401 | default: |
| 402 | if (i == 0 && saweval) { |
| 403 | return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0); |
| 404 | } |
| 405 | break; |
| 406 | case CXt_EVAL: |
| 407 | switch (cx->blk_eval.old_op_type) { |
| 408 | case OP_ENTEREVAL: |
| 409 | if (CxREALEVAL(cx)) { |
| 410 | PADOFFSET off; |
| 411 | saweval = i; |
| 412 | seq = cxstack[i].blk_oldcop->cop_seq; |
| 413 | startcv = cxstack[i].blk_eval.cv; |
| 414 | if (startcv && CvOUTSIDE(startcv)) { |
| 415 | off = pad_findlex(name, newoff, seq, CvOUTSIDE(startcv), |
| 416 | i-1, saweval, 0); |
| 417 | if (off) /* continue looking if not found here */ |
| 418 | return off; |
| 419 | } |
| 420 | } |
| 421 | break; |
| 422 | case OP_DOFILE: |
| 423 | case OP_REQUIRE: |
| 424 | /* require/do must have their own scope */ |
| 425 | return 0; |
| 426 | } |
| 427 | break; |
| 428 | case CXt_FORMAT: |
| 429 | case CXt_SUB: |
| 430 | if (!saweval) |
| 431 | return 0; |
| 432 | cv = cx->blk_sub.cv; |
| 433 | if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */ |
| 434 | saweval = i; /* so we know where we were called from */ |
| 435 | seq = cxstack[i].blk_oldcop->cop_seq; |
| 436 | continue; |
| 437 | } |
| 438 | return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | PADOFFSET |
| 446 | Perl_pad_findmy(pTHX_ char *name) |
| 447 | { |
| 448 | I32 off; |
| 449 | I32 pendoff = 0; |
| 450 | SV *sv; |
| 451 | SV **svp = AvARRAY(PL_comppad_name); |
| 452 | U32 seq = PL_cop_seqmax; |
| 453 | PERL_CONTEXT *cx; |
| 454 | CV *outside; |
| 455 | |
| 456 | #ifdef USE_5005THREADS |
| 457 | /* |
| 458 | * Special case to get lexical (and hence per-thread) @_. |
| 459 | * XXX I need to find out how to tell at parse-time whether use |
| 460 | * of @_ should refer to a lexical (from a sub) or defgv (global |
| 461 | * scope and maybe weird sub-ish things like formats). See |
| 462 | * startsub in perly.y. It's possible that @_ could be lexical |
| 463 | * (at least from subs) even in non-threaded perl. |
| 464 | */ |
| 465 | if (strEQ(name, "@_")) |
| 466 | return 0; /* success. (NOT_IN_PAD indicates failure) */ |
| 467 | #endif /* USE_5005THREADS */ |
| 468 | |
| 469 | /* The one we're looking for is probably just before comppad_name_fill. */ |
| 470 | for (off = AvFILLp(PL_comppad_name); off > 0; off--) { |
| 471 | if ((sv = svp[off]) && |
| 472 | sv != &PL_sv_undef && |
| 473 | (!SvIVX(sv) || |
| 474 | (seq <= (U32)SvIVX(sv) && |
| 475 | seq > (U32)I_32(SvNVX(sv)))) && |
| 476 | strEQ(SvPVX(sv), name)) |
| 477 | { |
| 478 | if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR) |
| 479 | return (PADOFFSET)off; |
| 480 | pendoff = off; /* this pending def. will override import */ |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | outside = CvOUTSIDE(PL_compcv); |
| 485 | |
| 486 | /* Check if if we're compiling an eval'', and adjust seq to be the |
| 487 | * eval's seq number. This depends on eval'' having a non-null |
| 488 | * CvOUTSIDE() while it is being compiled. The eval'' itself is |
| 489 | * identified by CvEVAL being true and CvGV being null. */ |
| 490 | if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) { |
| 491 | cx = &cxstack[cxstack_ix]; |
| 492 | if (CxREALEVAL(cx)) |
| 493 | seq = cx->blk_oldcop->cop_seq; |
| 494 | } |
| 495 | |
| 496 | /* See if it's in a nested scope */ |
| 497 | off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0); |
| 498 | if (off) { |
| 499 | /* If there is a pending local definition, this new alias must die */ |
| 500 | if (pendoff) |
| 501 | SvIVX(AvARRAY(PL_comppad_name)[off]) = seq; |
| 502 | return off; /* pad_findlex returns 0 for failure...*/ |
| 503 | } |
| 504 | return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */ |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | Perl_pad_leavemy(pTHX_ I32 fill) |
| 509 | { |
| 510 | I32 off; |
| 511 | SV **svp = AvARRAY(PL_comppad_name); |
| 512 | SV *sv; |
| 513 | if (PL_min_intro_pending && fill < PL_min_intro_pending) { |
| 514 | for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) { |
| 515 | if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL)) |
| 516 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "%s never introduced", SvPVX(sv)); |
| 517 | } |
| 518 | } |
| 519 | /* "Deintroduce" my variables that are leaving with this scope. */ |
| 520 | for (off = AvFILLp(PL_comppad_name); off > fill; off--) { |
| 521 | if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX) |
| 522 | SvIVX(sv) = PL_cop_seqmax; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | PADOFFSET |
| 527 | Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype) |
| 528 | { |
| 529 | SV *sv; |
| 530 | I32 retval; |
| 531 | |
| 532 | if (AvARRAY(PL_comppad) != PL_curpad) |
| 533 | Perl_croak(aTHX_ "panic: pad_alloc"); |
| 534 | if (PL_pad_reset_pending) |
| 535 | pad_reset(); |
| 536 | if (tmptype & SVs_PADMY) { |
| 537 | do { |
| 538 | sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE); |
| 539 | } while (SvPADBUSY(sv)); /* need a fresh one */ |
| 540 | retval = AvFILLp(PL_comppad); |
| 541 | } |
| 542 | else { |
| 543 | SV **names = AvARRAY(PL_comppad_name); |
| 544 | SSize_t names_fill = AvFILLp(PL_comppad_name); |
| 545 | for (;;) { |
| 546 | /* |
| 547 | * "foreach" index vars temporarily become aliases to non-"my" |
| 548 | * values. Thus we must skip, not just pad values that are |
| 549 | * marked as current pad values, but also those with names. |
| 550 | */ |
| 551 | if (++PL_padix <= names_fill && |
| 552 | (sv = names[PL_padix]) && sv != &PL_sv_undef) |
| 553 | continue; |
| 554 | sv = *av_fetch(PL_comppad, PL_padix, TRUE); |
| 555 | if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && |
| 556 | !IS_PADGV(sv) && !IS_PADCONST(sv)) |
| 557 | break; |
| 558 | } |
| 559 | retval = PL_padix; |
| 560 | } |
| 561 | SvFLAGS(sv) |= tmptype; |
| 562 | PL_curpad = AvARRAY(PL_comppad); |
| 563 | #ifdef USE_5005THREADS |
| 564 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 565 | "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n", |
| 566 | PTR2UV(thr), PTR2UV(PL_curpad), |
| 567 | (long) retval, PL_op_name[optype])); |
| 568 | #else |
| 569 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 570 | "Pad 0x%"UVxf" alloc %ld for %s\n", |
| 571 | PTR2UV(PL_curpad), |
| 572 | (long) retval, PL_op_name[optype])); |
| 573 | #endif /* USE_5005THREADS */ |
| 574 | return (PADOFFSET)retval; |
| 575 | } |
| 576 | |
| 577 | SV * |
| 578 | Perl_pad_sv(pTHX_ PADOFFSET po) |
| 579 | { |
| 580 | #ifdef USE_5005THREADS |
| 581 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 582 | "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n", |
| 583 | PTR2UV(thr), PTR2UV(PL_curpad), (IV)po)); |
| 584 | #else |
| 585 | if (!po) |
| 586 | Perl_croak(aTHX_ "panic: pad_sv po"); |
| 587 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n", |
| 588 | PTR2UV(PL_curpad), (IV)po)); |
| 589 | #endif /* USE_5005THREADS */ |
| 590 | return PL_curpad[po]; /* eventually we'll turn this into a macro */ |
| 591 | } |
| 592 | |
| 593 | void |
| 594 | Perl_pad_free(pTHX_ PADOFFSET po) |
| 595 | { |
| 596 | if (!PL_curpad) |
| 597 | return; |
| 598 | if (AvARRAY(PL_comppad) != PL_curpad) |
| 599 | Perl_croak(aTHX_ "panic: pad_free curpad"); |
| 600 | if (!po) |
| 601 | Perl_croak(aTHX_ "panic: pad_free po"); |
| 602 | #ifdef USE_5005THREADS |
| 603 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 604 | "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n", |
| 605 | PTR2UV(thr), PTR2UV(PL_curpad), (IV)po)); |
| 606 | #else |
| 607 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n", |
| 608 | PTR2UV(PL_curpad), (IV)po)); |
| 609 | #endif /* USE_5005THREADS */ |
| 610 | if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) { |
| 611 | SvPADTMP_off(PL_curpad[po]); |
| 612 | #ifdef USE_ITHREADS |
| 613 | SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */ |
| 614 | #endif |
| 615 | } |
| 616 | if ((I32)po < PL_padix) |
| 617 | PL_padix = po - 1; |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | Perl_pad_swipe(pTHX_ PADOFFSET po) |
| 622 | { |
| 623 | if (AvARRAY(PL_comppad) != PL_curpad) |
| 624 | Perl_croak(aTHX_ "panic: pad_swipe curpad"); |
| 625 | if (!po) |
| 626 | Perl_croak(aTHX_ "panic: pad_swipe po"); |
| 627 | #ifdef USE_5005THREADS |
| 628 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 629 | "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n", |
| 630 | PTR2UV(thr), PTR2UV(PL_curpad), (IV)po)); |
| 631 | #else |
| 632 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n", |
| 633 | PTR2UV(PL_curpad), (IV)po)); |
| 634 | #endif /* USE_5005THREADS */ |
| 635 | SvPADTMP_off(PL_curpad[po]); |
| 636 | PL_curpad[po] = NEWSV(1107,0); |
| 637 | SvPADTMP_on(PL_curpad[po]); |
| 638 | if ((I32)po < PL_padix) |
| 639 | PL_padix = po - 1; |
| 640 | } |
| 641 | |
| 642 | /* XXX pad_reset() is currently disabled because it results in serious bugs. |
| 643 | * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed |
| 644 | * on the stack by OPs that use them, there are several ways to get an alias |
| 645 | * to a shared TARG. Such an alias will change randomly and unpredictably. |
| 646 | * We avoid doing this until we can think of a Better Way. |
| 647 | * GSAR 97-10-29 */ |
| 648 | void |
| 649 | Perl_pad_reset(pTHX) |
| 650 | { |
| 651 | #ifdef USE_BROKEN_PAD_RESET |
| 652 | register I32 po; |
| 653 | |
| 654 | if (AvARRAY(PL_comppad) != PL_curpad) |
| 655 | Perl_croak(aTHX_ "panic: pad_reset curpad"); |
| 656 | #ifdef USE_5005THREADS |
| 657 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
| 658 | "0x%"UVxf" Pad 0x%"UVxf" reset\n", |
| 659 | PTR2UV(thr), PTR2UV(PL_curpad))); |
| 660 | #else |
| 661 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n", |
| 662 | PTR2UV(PL_curpad))); |
| 663 | #endif /* USE_5005THREADS */ |
| 664 | if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */ |
| 665 | for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) { |
| 666 | if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po])) |
| 667 | SvPADTMP_off(PL_curpad[po]); |
| 668 | } |
| 669 | PL_padix = PL_padix_floor; |
| 670 | } |
| 671 | #endif |
| 672 | PL_pad_reset_pending = FALSE; |
| 673 | } |
| 674 | |
| 675 | #ifdef USE_5005THREADS |
| 676 | /* find_threadsv is not reentrant */ |
| 677 | PADOFFSET |
| 678 | Perl_find_threadsv(pTHX_ const char *name) |
| 679 | { |
| 680 | char *p; |
| 681 | PADOFFSET key; |
| 682 | SV **svp; |
| 683 | /* We currently only handle names of a single character */ |
| 684 | p = strchr(PL_threadsv_names, *name); |
| 685 | if (!p) |
| 686 | return NOT_IN_PAD; |
| 687 | key = p - PL_threadsv_names; |
| 688 | MUTEX_LOCK(&thr->mutex); |
| 689 | svp = av_fetch(thr->threadsv, key, FALSE); |
| 690 | if (svp) |
| 691 | MUTEX_UNLOCK(&thr->mutex); |
| 692 | else { |
| 693 | SV *sv = NEWSV(0, 0); |
| 694 | av_store(thr->threadsv, key, sv); |
| 695 | thr->threadsvp = AvARRAY(thr->threadsv); |
| 696 | MUTEX_UNLOCK(&thr->mutex); |
| 697 | /* |
| 698 | * Some magic variables used to be automagically initialised |
| 699 | * in gv_fetchpv. Those which are now per-thread magicals get |
| 700 | * initialised here instead. |
| 701 | */ |
| 702 | switch (*name) { |
| 703 | case '_': |
| 704 | break; |
| 705 | case ';': |
| 706 | sv_setpv(sv, "\034"); |
| 707 | sv_magic(sv, 0, PERL_MAGIC_sv, name, 1); |
| 708 | break; |
| 709 | case '&': |
| 710 | case '`': |
| 711 | case '\'': |
| 712 | PL_sawampersand = TRUE; |
| 713 | /* FALL THROUGH */ |
| 714 | case '1': |
| 715 | case '2': |
| 716 | case '3': |
| 717 | case '4': |
| 718 | case '5': |
| 719 | case '6': |
| 720 | case '7': |
| 721 | case '8': |
| 722 | case '9': |
| 723 | SvREADONLY_on(sv); |
| 724 | /* FALL THROUGH */ |
| 725 | |
| 726 | /* XXX %! tied to Errno.pm needs to be added here. |
| 727 | * See gv_fetchpv(). */ |
| 728 | /* case '!': */ |
| 729 | |
| 730 | default: |
| 731 | sv_magic(sv, 0, PERL_MAGIC_sv, name, 1); |
| 732 | } |
| 733 | DEBUG_S(PerlIO_printf(Perl_error_log, |
| 734 | "find_threadsv: new SV %p for $%s%c\n", |
| 735 | sv, (*name < 32) ? "^" : "", |
| 736 | (*name < 32) ? toCTRL(*name) : *name)); |
| 737 | } |
| 738 | return key; |
| 739 | } |
| 740 | #endif /* USE_5005THREADS */ |
| 741 | |
| 742 | /* Destructor */ |
| 743 | |
| 744 | void |
| 745 | Perl_op_free(pTHX_ OP *o) |
| 746 | { |
| 747 | register OP *kid, *nextkid; |
| 748 | OPCODE type; |
| 749 | |
| 750 | if (!o || o->op_seq == (U16)-1) |
| 751 | return; |
| 752 | |
| 753 | if (o->op_private & OPpREFCOUNTED) { |
| 754 | switch (o->op_type) { |
| 755 | case OP_LEAVESUB: |
| 756 | case OP_LEAVESUBLV: |
| 757 | case OP_LEAVEEVAL: |
| 758 | case OP_LEAVE: |
| 759 | case OP_SCOPE: |
| 760 | case OP_LEAVEWRITE: |
| 761 | OP_REFCNT_LOCK; |
| 762 | if (OpREFCNT_dec(o)) { |
| 763 | OP_REFCNT_UNLOCK; |
| 764 | return; |
| 765 | } |
| 766 | OP_REFCNT_UNLOCK; |
| 767 | break; |
| 768 | default: |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (o->op_flags & OPf_KIDS) { |
| 774 | for (kid = cUNOPo->op_first; kid; kid = nextkid) { |
| 775 | nextkid = kid->op_sibling; /* Get before next freeing kid */ |
| 776 | op_free(kid); |
| 777 | } |
| 778 | } |
| 779 | type = o->op_type; |
| 780 | if (type == OP_NULL) |
| 781 | type = (OPCODE)o->op_targ; |
| 782 | |
| 783 | /* COP* is not cleared by op_clear() so that we may track line |
| 784 | * numbers etc even after null() */ |
| 785 | if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE) |
| 786 | cop_free((COP*)o); |
| 787 | |
| 788 | op_clear(o); |
| 789 | FreeOp(o); |
| 790 | } |
| 791 | |
| 792 | void |
| 793 | Perl_op_clear(pTHX_ OP *o) |
| 794 | { |
| 795 | |
| 796 | switch (o->op_type) { |
| 797 | case OP_NULL: /* Was holding old type, if any. */ |
| 798 | case OP_ENTEREVAL: /* Was holding hints. */ |
| 799 | #ifdef USE_5005THREADS |
| 800 | case OP_THREADSV: /* Was holding index into thr->threadsv AV. */ |
| 801 | #endif |
| 802 | o->op_targ = 0; |
| 803 | break; |
| 804 | #ifdef USE_5005THREADS |
| 805 | case OP_ENTERITER: |
| 806 | if (!(o->op_flags & OPf_SPECIAL)) |
| 807 | break; |
| 808 | /* FALL THROUGH */ |
| 809 | #endif /* USE_5005THREADS */ |
| 810 | default: |
| 811 | if (!(o->op_flags & OPf_REF) |
| 812 | || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst))) |
| 813 | break; |
| 814 | /* FALL THROUGH */ |
| 815 | case OP_GVSV: |
| 816 | case OP_GV: |
| 817 | case OP_AELEMFAST: |
| 818 | #ifdef USE_ITHREADS |
| 819 | if (cPADOPo->op_padix > 0) { |
| 820 | if (PL_curpad) { |
| 821 | GV *gv = cGVOPo_gv; |
| 822 | pad_swipe(cPADOPo->op_padix); |
| 823 | /* No GvIN_PAD_off(gv) here, because other references may still |
| 824 | * exist on the pad */ |
| 825 | SvREFCNT_dec(gv); |
| 826 | } |
| 827 | cPADOPo->op_padix = 0; |
| 828 | } |
| 829 | #else |
| 830 | SvREFCNT_dec(cSVOPo->op_sv); |
| 831 | cSVOPo->op_sv = Nullsv; |
| 832 | #endif |
| 833 | break; |
| 834 | case OP_METHOD_NAMED: |
| 835 | case OP_CONST: |
| 836 | SvREFCNT_dec(cSVOPo->op_sv); |
| 837 | cSVOPo->op_sv = Nullsv; |
| 838 | break; |
| 839 | case OP_GOTO: |
| 840 | case OP_NEXT: |
| 841 | case OP_LAST: |
| 842 | case OP_REDO: |
| 843 | if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) |
| 844 | break; |
| 845 | /* FALL THROUGH */ |
| 846 | case OP_TRANS: |
| 847 | if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { |
| 848 | SvREFCNT_dec(cSVOPo->op_sv); |
| 849 | cSVOPo->op_sv = Nullsv; |
| 850 | } |
| 851 | else { |
| 852 | Safefree(cPVOPo->op_pv); |
| 853 | cPVOPo->op_pv = Nullch; |
| 854 | } |
| 855 | break; |
| 856 | case OP_SUBST: |
| 857 | op_free(cPMOPo->op_pmreplroot); |
| 858 | goto clear_pmop; |
| 859 | case OP_PUSHRE: |
| 860 | #ifdef USE_ITHREADS |
| 861 | if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) { |
| 862 | if (PL_curpad) { |
| 863 | GV *gv = (GV*)PL_curpad[INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)]; |
| 864 | pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)); |
| 865 | /* No GvIN_PAD_off(gv) here, because other references may still |
| 866 | * exist on the pad */ |
| 867 | SvREFCNT_dec(gv); |
| 868 | } |
| 869 | } |
| 870 | #else |
| 871 | SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot); |
| 872 | #endif |
| 873 | /* FALL THROUGH */ |
| 874 | case OP_MATCH: |
| 875 | case OP_QR: |
| 876 | clear_pmop: |
| 877 | { |
| 878 | HV *pmstash = PmopSTASH(cPMOPo); |
| 879 | if (pmstash && SvREFCNT(pmstash)) { |
| 880 | PMOP *pmop = HvPMROOT(pmstash); |
| 881 | PMOP *lastpmop = NULL; |
| 882 | while (pmop) { |
| 883 | if (cPMOPo == pmop) { |
| 884 | if (lastpmop) |
| 885 | lastpmop->op_pmnext = pmop->op_pmnext; |
| 886 | else |
| 887 | HvPMROOT(pmstash) = pmop->op_pmnext; |
| 888 | break; |
| 889 | } |
| 890 | lastpmop = pmop; |
| 891 | pmop = pmop->op_pmnext; |
| 892 | } |
| 893 | } |
| 894 | PmopSTASH_free(cPMOPo); |
| 895 | } |
| 896 | cPMOPo->op_pmreplroot = Nullop; |
| 897 | /* we use the "SAFE" version of the PM_ macros here |
| 898 | * since sv_clean_all might release some PMOPs |
| 899 | * after PL_regex_padav has been cleared |
| 900 | * and the clearing of PL_regex_padav needs to |
| 901 | * happen before sv_clean_all |
| 902 | */ |
| 903 | ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo)); |
| 904 | PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL); |
| 905 | #ifdef USE_ITHREADS |
| 906 | if(PL_regex_pad) { /* We could be in destruction */ |
| 907 | av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]); |
| 908 | SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]); |
| 909 | PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset); |
| 910 | } |
| 911 | #endif |
| 912 | |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | if (o->op_targ > 0) { |
| 917 | pad_free(o->op_targ); |
| 918 | o->op_targ = 0; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | STATIC void |
| 923 | S_cop_free(pTHX_ COP* cop) |
| 924 | { |
| 925 | Safefree(cop->cop_label); /* FIXME: treaddead ??? */ |
| 926 | CopFILE_free(cop); |
| 927 | CopSTASH_free(cop); |
| 928 | if (! specialWARN(cop->cop_warnings)) |
| 929 | SvREFCNT_dec(cop->cop_warnings); |
| 930 | if (! specialCopIO(cop->cop_io)) { |
| 931 | #ifdef USE_ITHREADS |
| 932 | #if 0 |
| 933 | STRLEN len; |
| 934 | char *s = SvPV(cop->cop_io,len); |
| 935 | Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */ |
| 936 | #endif |
| 937 | #else |
| 938 | SvREFCNT_dec(cop->cop_io); |
| 939 | #endif |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | void |
| 944 | Perl_op_null(pTHX_ OP *o) |
| 945 | { |
| 946 | if (o->op_type == OP_NULL) |
| 947 | return; |
| 948 | op_clear(o); |
| 949 | o->op_targ = o->op_type; |
| 950 | o->op_type = OP_NULL; |
| 951 | o->op_ppaddr = PL_ppaddr[OP_NULL]; |
| 952 | } |
| 953 | |
| 954 | /* Contextualizers */ |
| 955 | |
| 956 | #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o)) |
| 957 | |
| 958 | OP * |
| 959 | Perl_linklist(pTHX_ OP *o) |
| 960 | { |
| 961 | register OP *kid; |
| 962 | |
| 963 | if (o->op_next) |
| 964 | return o->op_next; |
| 965 | |
| 966 | /* establish postfix order */ |
| 967 | if (cUNOPo->op_first) { |
| 968 | o->op_next = LINKLIST(cUNOPo->op_first); |
| 969 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { |
| 970 | if (kid->op_sibling) |
| 971 | kid->op_next = LINKLIST(kid->op_sibling); |
| 972 | else |
| 973 | kid->op_next = o; |
| 974 | } |
| 975 | } |
| 976 | else |
| 977 | o->op_next = o; |
| 978 | |
| 979 | return o->op_next; |
| 980 | } |
| 981 | |
| 982 | OP * |
| 983 | Perl_scalarkids(pTHX_ OP *o) |
| 984 | { |
| 985 | OP *kid; |
| 986 | if (o && o->op_flags & OPf_KIDS) { |
| 987 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 988 | scalar(kid); |
| 989 | } |
| 990 | return o; |
| 991 | } |
| 992 | |
| 993 | STATIC OP * |
| 994 | S_scalarboolean(pTHX_ OP *o) |
| 995 | { |
| 996 | if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) { |
| 997 | if (ckWARN(WARN_SYNTAX)) { |
| 998 | line_t oldline = CopLINE(PL_curcop); |
| 999 | |
| 1000 | if (PL_copline != NOLINE) |
| 1001 | CopLINE_set(PL_curcop, PL_copline); |
| 1002 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be =="); |
| 1003 | CopLINE_set(PL_curcop, oldline); |
| 1004 | } |
| 1005 | } |
| 1006 | return scalar(o); |
| 1007 | } |
| 1008 | |
| 1009 | OP * |
| 1010 | Perl_scalar(pTHX_ OP *o) |
| 1011 | { |
| 1012 | OP *kid; |
| 1013 | |
| 1014 | /* assumes no premature commitment */ |
| 1015 | if (!o || (o->op_flags & OPf_WANT) || PL_error_count |
| 1016 | || o->op_type == OP_RETURN) |
| 1017 | { |
| 1018 | return o; |
| 1019 | } |
| 1020 | |
| 1021 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; |
| 1022 | |
| 1023 | switch (o->op_type) { |
| 1024 | case OP_REPEAT: |
| 1025 | scalar(cBINOPo->op_first); |
| 1026 | break; |
| 1027 | case OP_OR: |
| 1028 | case OP_AND: |
| 1029 | case OP_COND_EXPR: |
| 1030 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 1031 | scalar(kid); |
| 1032 | break; |
| 1033 | case OP_SPLIT: |
| 1034 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
| 1035 | if (!kPMOP->op_pmreplroot) |
| 1036 | deprecate_old("implicit split to @_"); |
| 1037 | } |
| 1038 | /* FALL THROUGH */ |
| 1039 | case OP_MATCH: |
| 1040 | case OP_QR: |
| 1041 | case OP_SUBST: |
| 1042 | case OP_NULL: |
| 1043 | default: |
| 1044 | if (o->op_flags & OPf_KIDS) { |
| 1045 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) |
| 1046 | scalar(kid); |
| 1047 | } |
| 1048 | break; |
| 1049 | case OP_LEAVE: |
| 1050 | case OP_LEAVETRY: |
| 1051 | kid = cLISTOPo->op_first; |
| 1052 | scalar(kid); |
| 1053 | while ((kid = kid->op_sibling)) { |
| 1054 | if (kid->op_sibling) |
| 1055 | scalarvoid(kid); |
| 1056 | else |
| 1057 | scalar(kid); |
| 1058 | } |
| 1059 | WITH_THR(PL_curcop = &PL_compiling); |
| 1060 | break; |
| 1061 | case OP_SCOPE: |
| 1062 | case OP_LINESEQ: |
| 1063 | case OP_LIST: |
| 1064 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
| 1065 | if (kid->op_sibling) |
| 1066 | scalarvoid(kid); |
| 1067 | else |
| 1068 | scalar(kid); |
| 1069 | } |
| 1070 | WITH_THR(PL_curcop = &PL_compiling); |
| 1071 | break; |
| 1072 | case OP_SORT: |
| 1073 | if (ckWARN(WARN_VOID)) |
| 1074 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context"); |
| 1075 | } |
| 1076 | return o; |
| 1077 | } |
| 1078 | |
| 1079 | OP * |
| 1080 | Perl_scalarvoid(pTHX_ OP *o) |
| 1081 | { |
| 1082 | OP *kid; |
| 1083 | char* useless = 0; |
| 1084 | SV* sv; |
| 1085 | U8 want; |
| 1086 | |
| 1087 | if (o->op_type == OP_NEXTSTATE |
| 1088 | || o->op_type == OP_SETSTATE |
| 1089 | || o->op_type == OP_DBSTATE |
| 1090 | || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE |
| 1091 | || o->op_targ == OP_SETSTATE |
| 1092 | || o->op_targ == OP_DBSTATE))) |
| 1093 | PL_curcop = (COP*)o; /* for warning below */ |
| 1094 | |
| 1095 | /* assumes no premature commitment */ |
| 1096 | want = o->op_flags & OPf_WANT; |
| 1097 | if ((want && want != OPf_WANT_SCALAR) || PL_error_count |
| 1098 | || o->op_type == OP_RETURN) |
| 1099 | { |
| 1100 | return o; |
| 1101 | } |
| 1102 | |
| 1103 | if ((o->op_private & OPpTARGET_MY) |
| 1104 | && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ |
| 1105 | { |
| 1106 | return scalar(o); /* As if inside SASSIGN */ |
| 1107 | } |
| 1108 | |
| 1109 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; |
| 1110 | |
| 1111 | switch (o->op_type) { |
| 1112 | default: |
| 1113 | if (!(PL_opargs[o->op_type] & OA_FOLDCONST)) |
| 1114 | break; |
| 1115 | /* FALL THROUGH */ |
| 1116 | case OP_REPEAT: |
| 1117 | if (o->op_flags & OPf_STACKED) |
| 1118 | break; |
| 1119 | goto func_ops; |
| 1120 | case OP_SUBSTR: |
| 1121 | if (o->op_private == 4) |
| 1122 | break; |
| 1123 | /* FALL THROUGH */ |
| 1124 | case OP_GVSV: |
| 1125 | case OP_WANTARRAY: |
| 1126 | case OP_GV: |
| 1127 | case OP_PADSV: |
| 1128 | case OP_PADAV: |
| 1129 | case OP_PADHV: |
| 1130 | case OP_PADANY: |
| 1131 | case OP_AV2ARYLEN: |
| 1132 | case OP_REF: |
| 1133 | case OP_REFGEN: |
| 1134 | case OP_SREFGEN: |
| 1135 | case OP_DEFINED: |
| 1136 | case OP_HEX: |
| 1137 | case OP_OCT: |
| 1138 | case OP_LENGTH: |
| 1139 | case OP_VEC: |
| 1140 | case OP_INDEX: |
| 1141 | case OP_RINDEX: |
| 1142 | case OP_SPRINTF: |
| 1143 | case OP_AELEM: |
| 1144 | case OP_AELEMFAST: |
| 1145 | case OP_ASLICE: |
| 1146 | case OP_HELEM: |
| 1147 | case OP_HSLICE: |
| 1148 | case OP_UNPACK: |
| 1149 | case OP_PACK: |
| 1150 | case OP_JOIN: |
| 1151 | case OP_LSLICE: |
| 1152 | case OP_ANONLIST: |
| 1153 | case OP_ANONHASH: |
| 1154 | case OP_SORT: |
| 1155 | case OP_REVERSE: |
| 1156 | case OP_RANGE: |
| 1157 | case OP_FLIP: |
| 1158 | case OP_FLOP: |
| 1159 | case OP_CALLER: |
| 1160 | case OP_FILENO: |
| 1161 | case OP_EOF: |
| 1162 | case OP_TELL: |
| 1163 | case OP_GETSOCKNAME: |
| 1164 | case OP_GETPEERNAME: |
| 1165 | case OP_READLINK: |
| 1166 | case OP_TELLDIR: |
| 1167 | case OP_GETPPID: |
| 1168 | case OP_GETPGRP: |
| 1169 | case OP_GETPRIORITY: |
| 1170 | case OP_TIME: |
| 1171 | case OP_TMS: |
| 1172 | case OP_LOCALTIME: |
| 1173 | case OP_GMTIME: |
| 1174 | case OP_GHBYNAME: |
| 1175 | case OP_GHBYADDR: |
| 1176 | case OP_GHOSTENT: |
| 1177 | case OP_GNBYNAME: |
| 1178 | case OP_GNBYADDR: |
| 1179 | case OP_GNETENT: |
| 1180 | case OP_GPBYNAME: |
| 1181 | case OP_GPBYNUMBER: |
| 1182 | case OP_GPROTOENT: |
| 1183 | case OP_GSBYNAME: |
| 1184 | case OP_GSBYPORT: |
| 1185 | case OP_GSERVENT: |
| 1186 | case OP_GPWNAM: |
| 1187 | case OP_GPWUID: |
| 1188 | case OP_GGRNAM: |
| 1189 | case OP_GGRGID: |
| 1190 | case OP_GETLOGIN: |
| 1191 | func_ops: |
| 1192 | if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))) |
| 1193 | useless = OP_DESC(o); |
| 1194 | break; |
| 1195 | |
| 1196 | case OP_RV2GV: |
| 1197 | case OP_RV2SV: |
| 1198 | case OP_RV2AV: |
| 1199 | case OP_RV2HV: |
| 1200 | if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) && |
| 1201 | (!o->op_sibling || o->op_sibling->op_type != OP_READLINE)) |
| 1202 | useless = "a variable"; |
| 1203 | break; |
| 1204 | |
| 1205 | case OP_CONST: |
| 1206 | sv = cSVOPo_sv; |
| 1207 | if (cSVOPo->op_private & OPpCONST_STRICT) |
| 1208 | no_bareword_allowed(o); |
| 1209 | else { |
| 1210 | if (ckWARN(WARN_VOID)) { |
| 1211 | useless = "a constant"; |
| 1212 | /* the constants 0 and 1 are permitted as they are |
| 1213 | conventionally used as dummies in constructs like |
| 1214 | 1 while some_condition_with_side_effects; */ |
| 1215 | if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) |
| 1216 | useless = 0; |
| 1217 | else if (SvPOK(sv)) { |
| 1218 | /* perl4's way of mixing documentation and code |
| 1219 | (before the invention of POD) was based on a |
| 1220 | trick to mix nroff and perl code. The trick was |
| 1221 | built upon these three nroff macros being used in |
| 1222 | void context. The pink camel has the details in |
| 1223 | the script wrapman near page 319. */ |
| 1224 | if (strnEQ(SvPVX(sv), "di", 2) || |
| 1225 | strnEQ(SvPVX(sv), "ds", 2) || |
| 1226 | strnEQ(SvPVX(sv), "ig", 2)) |
| 1227 | useless = 0; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | op_null(o); /* don't execute or even remember it */ |
| 1232 | break; |
| 1233 | |
| 1234 | case OP_POSTINC: |
| 1235 | o->op_type = OP_PREINC; /* pre-increment is faster */ |
| 1236 | o->op_ppaddr = PL_ppaddr[OP_PREINC]; |
| 1237 | break; |
| 1238 | |
| 1239 | case OP_POSTDEC: |
| 1240 | o->op_type = OP_PREDEC; /* pre-decrement is faster */ |
| 1241 | o->op_ppaddr = PL_ppaddr[OP_PREDEC]; |
| 1242 | break; |
| 1243 | |
| 1244 | case OP_OR: |
| 1245 | case OP_AND: |
| 1246 | case OP_COND_EXPR: |
| 1247 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 1248 | scalarvoid(kid); |
| 1249 | break; |
| 1250 | |
| 1251 | case OP_NULL: |
| 1252 | if (o->op_flags & OPf_STACKED) |
| 1253 | break; |
| 1254 | /* FALL THROUGH */ |
| 1255 | case OP_NEXTSTATE: |
| 1256 | case OP_DBSTATE: |
| 1257 | case OP_ENTERTRY: |
| 1258 | case OP_ENTER: |
| 1259 | if (!(o->op_flags & OPf_KIDS)) |
| 1260 | break; |
| 1261 | /* FALL THROUGH */ |
| 1262 | case OP_SCOPE: |
| 1263 | case OP_LEAVE: |
| 1264 | case OP_LEAVETRY: |
| 1265 | case OP_LEAVELOOP: |
| 1266 | case OP_LINESEQ: |
| 1267 | case OP_LIST: |
| 1268 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 1269 | scalarvoid(kid); |
| 1270 | break; |
| 1271 | case OP_ENTEREVAL: |
| 1272 | scalarkids(o); |
| 1273 | break; |
| 1274 | case OP_REQUIRE: |
| 1275 | /* all requires must return a boolean value */ |
| 1276 | o->op_flags &= ~OPf_WANT; |
| 1277 | /* FALL THROUGH */ |
| 1278 | case OP_SCALAR: |
| 1279 | return scalar(o); |
| 1280 | case OP_SPLIT: |
| 1281 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
| 1282 | if (!kPMOP->op_pmreplroot) |
| 1283 | deprecate_old("implicit split to @_"); |
| 1284 | } |
| 1285 | break; |
| 1286 | } |
| 1287 | if (useless && ckWARN(WARN_VOID)) |
| 1288 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless); |
| 1289 | return o; |
| 1290 | } |
| 1291 | |
| 1292 | OP * |
| 1293 | Perl_listkids(pTHX_ OP *o) |
| 1294 | { |
| 1295 | OP *kid; |
| 1296 | if (o && o->op_flags & OPf_KIDS) { |
| 1297 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 1298 | list(kid); |
| 1299 | } |
| 1300 | return o; |
| 1301 | } |
| 1302 | |
| 1303 | OP * |
| 1304 | Perl_list(pTHX_ OP *o) |
| 1305 | { |
| 1306 | OP *kid; |
| 1307 | |
| 1308 | /* assumes no premature commitment */ |
| 1309 | if (!o || (o->op_flags & OPf_WANT) || PL_error_count |
| 1310 | || o->op_type == OP_RETURN) |
| 1311 | { |
| 1312 | return o; |
| 1313 | } |
| 1314 | |
| 1315 | if ((o->op_private & OPpTARGET_MY) |
| 1316 | && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ |
| 1317 | { |
| 1318 | return o; /* As if inside SASSIGN */ |
| 1319 | } |
| 1320 | |
| 1321 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; |
| 1322 | |
| 1323 | switch (o->op_type) { |
| 1324 | case OP_FLOP: |
| 1325 | case OP_REPEAT: |
| 1326 | list(cBINOPo->op_first); |
| 1327 | break; |
| 1328 | case OP_OR: |
| 1329 | case OP_AND: |
| 1330 | case OP_COND_EXPR: |
| 1331 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 1332 | list(kid); |
| 1333 | break; |
| 1334 | default: |
| 1335 | case OP_MATCH: |
| 1336 | case OP_QR: |
| 1337 | case OP_SUBST: |
| 1338 | case OP_NULL: |
| 1339 | if (!(o->op_flags & OPf_KIDS)) |
| 1340 | break; |
| 1341 | if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { |
| 1342 | list(cBINOPo->op_first); |
| 1343 | return gen_constant_list(o); |
| 1344 | } |
| 1345 | case OP_LIST: |
| 1346 | listkids(o); |
| 1347 | break; |
| 1348 | case OP_LEAVE: |
| 1349 | case OP_LEAVETRY: |
| 1350 | kid = cLISTOPo->op_first; |
| 1351 | list(kid); |
| 1352 | while ((kid = kid->op_sibling)) { |
| 1353 | if (kid->op_sibling) |
| 1354 | scalarvoid(kid); |
| 1355 | else |
| 1356 | list(kid); |
| 1357 | } |
| 1358 | WITH_THR(PL_curcop = &PL_compiling); |
| 1359 | break; |
| 1360 | case OP_SCOPE: |
| 1361 | case OP_LINESEQ: |
| 1362 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
| 1363 | if (kid->op_sibling) |
| 1364 | scalarvoid(kid); |
| 1365 | else |
| 1366 | list(kid); |
| 1367 | } |
| 1368 | WITH_THR(PL_curcop = &PL_compiling); |
| 1369 | break; |
| 1370 | case OP_REQUIRE: |
| 1371 | /* all requires must return a boolean value */ |
| 1372 | o->op_flags &= ~OPf_WANT; |
| 1373 | return scalar(o); |
| 1374 | } |
| 1375 | return o; |
| 1376 | } |
| 1377 | |
| 1378 | OP * |
| 1379 | Perl_scalarseq(pTHX_ OP *o) |
| 1380 | { |
| 1381 | OP *kid; |
| 1382 | |
| 1383 | if (o) { |
| 1384 | if (o->op_type == OP_LINESEQ || |
| 1385 | o->op_type == OP_SCOPE || |
| 1386 | o->op_type == OP_LEAVE || |
| 1387 | o->op_type == OP_LEAVETRY) |
| 1388 | { |
| 1389 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
| 1390 | if (kid->op_sibling) { |
| 1391 | scalarvoid(kid); |
| 1392 | } |
| 1393 | } |
| 1394 | PL_curcop = &PL_compiling; |
| 1395 | } |
| 1396 | o->op_flags &= ~OPf_PARENS; |
| 1397 | if (PL_hints & HINT_BLOCK_SCOPE) |
| 1398 | o->op_flags |= OPf_PARENS; |
| 1399 | } |
| 1400 | else |
| 1401 | o = newOP(OP_STUB, 0); |
| 1402 | return o; |
| 1403 | } |
| 1404 | |
| 1405 | STATIC OP * |
| 1406 | S_modkids(pTHX_ OP *o, I32 type) |
| 1407 | { |
| 1408 | OP *kid; |
| 1409 | if (o && o->op_flags & OPf_KIDS) { |
| 1410 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 1411 | mod(kid, type); |
| 1412 | } |
| 1413 | return o; |
| 1414 | } |
| 1415 | |
| 1416 | OP * |
| 1417 | Perl_mod(pTHX_ OP *o, I32 type) |
| 1418 | { |
| 1419 | OP *kid; |
| 1420 | STRLEN n_a; |
| 1421 | |
| 1422 | if (!o || PL_error_count) |
| 1423 | return o; |
| 1424 | |
| 1425 | if ((o->op_private & OPpTARGET_MY) |
| 1426 | && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ |
| 1427 | { |
| 1428 | return o; |
| 1429 | } |
| 1430 | |
| 1431 | switch (o->op_type) { |
| 1432 | case OP_UNDEF: |
| 1433 | PL_modcount++; |
| 1434 | return o; |
| 1435 | case OP_CONST: |
| 1436 | if (!(o->op_private & (OPpCONST_ARYBASE))) |
| 1437 | goto nomod; |
| 1438 | if (PL_eval_start && PL_eval_start->op_type == OP_CONST) { |
| 1439 | PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv); |
| 1440 | PL_eval_start = 0; |
| 1441 | } |
| 1442 | else if (!type) { |
| 1443 | SAVEI32(PL_compiling.cop_arybase); |
| 1444 | PL_compiling.cop_arybase = 0; |
| 1445 | } |
| 1446 | else if (type == OP_REFGEN) |
| 1447 | goto nomod; |
| 1448 | else |
| 1449 | Perl_croak(aTHX_ "That use of $[ is unsupported"); |
| 1450 | break; |
| 1451 | case OP_STUB: |
| 1452 | if (o->op_flags & OPf_PARENS) |
| 1453 | break; |
| 1454 | goto nomod; |
| 1455 | case OP_ENTERSUB: |
| 1456 | if ((type == OP_UNDEF || type == OP_REFGEN) && |
| 1457 | !(o->op_flags & OPf_STACKED)) { |
| 1458 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ |
| 1459 | o->op_ppaddr = PL_ppaddr[OP_RV2CV]; |
| 1460 | assert(cUNOPo->op_first->op_type == OP_NULL); |
| 1461 | op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ |
| 1462 | break; |
| 1463 | } |
| 1464 | else if (o->op_private & OPpENTERSUB_NOMOD) |
| 1465 | return o; |
| 1466 | else { /* lvalue subroutine call */ |
| 1467 | o->op_private |= OPpLVAL_INTRO; |
| 1468 | PL_modcount = RETURN_UNLIMITED_NUMBER; |
| 1469 | if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) { |
| 1470 | /* Backward compatibility mode: */ |
| 1471 | o->op_private |= OPpENTERSUB_INARGS; |
| 1472 | break; |
| 1473 | } |
| 1474 | else { /* Compile-time error message: */ |
| 1475 | OP *kid = cUNOPo->op_first; |
| 1476 | CV *cv; |
| 1477 | OP *okid; |
| 1478 | |
| 1479 | if (kid->op_type == OP_PUSHMARK) |
| 1480 | goto skip_kids; |
| 1481 | if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST) |
| 1482 | Perl_croak(aTHX_ |
| 1483 | "panic: unexpected lvalue entersub " |
| 1484 | "args: type/targ %ld:%"UVuf, |
| 1485 | (long)kid->op_type, (UV)kid->op_targ); |
| 1486 | kid = kLISTOP->op_first; |
| 1487 | skip_kids: |
| 1488 | while (kid->op_sibling) |
| 1489 | kid = kid->op_sibling; |
| 1490 | if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) { |
| 1491 | /* Indirect call */ |
| 1492 | if (kid->op_type == OP_METHOD_NAMED |
| 1493 | || kid->op_type == OP_METHOD) |
| 1494 | { |
| 1495 | UNOP *newop; |
| 1496 | |
| 1497 | NewOp(1101, newop, 1, UNOP); |
| 1498 | newop->op_type = OP_RV2CV; |
| 1499 | newop->op_ppaddr = PL_ppaddr[OP_RV2CV]; |
| 1500 | newop->op_first = Nullop; |
| 1501 | newop->op_next = (OP*)newop; |
| 1502 | kid->op_sibling = (OP*)newop; |
| 1503 | newop->op_private |= OPpLVAL_INTRO; |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | if (kid->op_type != OP_RV2CV) |
| 1508 | Perl_croak(aTHX_ |
| 1509 | "panic: unexpected lvalue entersub " |
| 1510 | "entry via type/targ %ld:%"UVuf, |
| 1511 | (long)kid->op_type, (UV)kid->op_targ); |
| 1512 | kid->op_private |= OPpLVAL_INTRO; |
| 1513 | break; /* Postpone until runtime */ |
| 1514 | } |
| 1515 | |
| 1516 | okid = kid; |
| 1517 | kid = kUNOP->op_first; |
| 1518 | if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV) |
| 1519 | kid = kUNOP->op_first; |
| 1520 | if (kid->op_type == OP_NULL) |
| 1521 | Perl_croak(aTHX_ |
| 1522 | "Unexpected constant lvalue entersub " |
| 1523 | "entry via type/targ %ld:%"UVuf, |
| 1524 | (long)kid->op_type, (UV)kid->op_targ); |
| 1525 | if (kid->op_type != OP_GV) { |
| 1526 | /* Restore RV2CV to check lvalueness */ |
| 1527 | restore_2cv: |
| 1528 | if (kid->op_next && kid->op_next != kid) { /* Happens? */ |
| 1529 | okid->op_next = kid->op_next; |
| 1530 | kid->op_next = okid; |
| 1531 | } |
| 1532 | else |
| 1533 | okid->op_next = Nullop; |
| 1534 | okid->op_type = OP_RV2CV; |
| 1535 | okid->op_targ = 0; |
| 1536 | okid->op_ppaddr = PL_ppaddr[OP_RV2CV]; |
| 1537 | okid->op_private |= OPpLVAL_INTRO; |
| 1538 | break; |
| 1539 | } |
| 1540 | |
| 1541 | cv = GvCV(kGVOP_gv); |
| 1542 | if (!cv) |
| 1543 | goto restore_2cv; |
| 1544 | if (CvLVALUE(cv)) |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | /* FALL THROUGH */ |
| 1549 | default: |
| 1550 | nomod: |
| 1551 | /* grep, foreach, subcalls, refgen */ |
| 1552 | if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) |
| 1553 | break; |
| 1554 | yyerror(Perl_form(aTHX_ "Can't modify %s in %s", |
| 1555 | (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL) |
| 1556 | ? "do block" |
| 1557 | : (o->op_type == OP_ENTERSUB |
| 1558 | ? "non-lvalue subroutine call" |
| 1559 | : OP_DESC(o))), |
| 1560 | type ? PL_op_desc[type] : "local")); |
| 1561 | return o; |
| 1562 | |
| 1563 | case OP_PREINC: |
| 1564 | case OP_PREDEC: |
| 1565 | case OP_POW: |
| 1566 | case OP_MULTIPLY: |
| 1567 | case OP_DIVIDE: |
| 1568 | case OP_MODULO: |
| 1569 | case OP_REPEAT: |
| 1570 | case OP_ADD: |
| 1571 | case OP_SUBTRACT: |
| 1572 | case OP_CONCAT: |
| 1573 | case OP_LEFT_SHIFT: |
| 1574 | case OP_RIGHT_SHIFT: |
| 1575 | case OP_BIT_AND: |
| 1576 | case OP_BIT_XOR: |
| 1577 | case OP_BIT_OR: |
| 1578 | case OP_I_MULTIPLY: |
| 1579 | case OP_I_DIVIDE: |
| 1580 | case OP_I_MODULO: |
| 1581 | case OP_I_ADD: |
| 1582 | case OP_I_SUBTRACT: |
| 1583 | if (!(o->op_flags & OPf_STACKED)) |
| 1584 | goto nomod; |
| 1585 | PL_modcount++; |
| 1586 | break; |
| 1587 | |
| 1588 | case OP_COND_EXPR: |
| 1589 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 1590 | mod(kid, type); |
| 1591 | break; |
| 1592 | |
| 1593 | case OP_RV2AV: |
| 1594 | case OP_RV2HV: |
| 1595 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
| 1596 | Perl_croak(aTHX_ "Can't localize through a reference"); |
| 1597 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { |
| 1598 | PL_modcount = RETURN_UNLIMITED_NUMBER; |
| 1599 | return o; /* Treat \(@foo) like ordinary list. */ |
| 1600 | } |
| 1601 | /* FALL THROUGH */ |
| 1602 | case OP_RV2GV: |
| 1603 | if (scalar_mod_type(o, type)) |
| 1604 | goto nomod; |
| 1605 | ref(cUNOPo->op_first, o->op_type); |
| 1606 | /* FALL THROUGH */ |
| 1607 | case OP_ASLICE: |
| 1608 | case OP_HSLICE: |
| 1609 | if (type == OP_LEAVESUBLV) |
| 1610 | o->op_private |= OPpMAYBE_LVSUB; |
| 1611 | /* FALL THROUGH */ |
| 1612 | case OP_AASSIGN: |
| 1613 | case OP_NEXTSTATE: |
| 1614 | case OP_DBSTATE: |
| 1615 | case OP_CHOMP: |
| 1616 | PL_modcount = RETURN_UNLIMITED_NUMBER; |
| 1617 | break; |
| 1618 | case OP_RV2SV: |
| 1619 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
| 1620 | Perl_croak(aTHX_ "Can't localize through a reference"); |
| 1621 | ref(cUNOPo->op_first, o->op_type); |
| 1622 | /* FALL THROUGH */ |
| 1623 | case OP_GV: |
| 1624 | case OP_AV2ARYLEN: |
| 1625 | PL_hints |= HINT_BLOCK_SCOPE; |
| 1626 | case OP_SASSIGN: |
| 1627 | case OP_ANDASSIGN: |
| 1628 | case OP_ORASSIGN: |
| 1629 | case OP_AELEMFAST: |
| 1630 | PL_modcount++; |
| 1631 | break; |
| 1632 | |
| 1633 | case OP_PADAV: |
| 1634 | case OP_PADHV: |
| 1635 | PL_modcount = RETURN_UNLIMITED_NUMBER; |
| 1636 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) |
| 1637 | return o; /* Treat \(@foo) like ordinary list. */ |
| 1638 | if (scalar_mod_type(o, type)) |
| 1639 | goto nomod; |
| 1640 | if (type == OP_LEAVESUBLV) |
| 1641 | o->op_private |= OPpMAYBE_LVSUB; |
| 1642 | /* FALL THROUGH */ |
| 1643 | case OP_PADSV: |
| 1644 | PL_modcount++; |
| 1645 | if (!type) |
| 1646 | Perl_croak(aTHX_ "Can't localize lexical variable %s", |
| 1647 | SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a)); |
| 1648 | break; |
| 1649 | |
| 1650 | #ifdef USE_5005THREADS |
| 1651 | case OP_THREADSV: |
| 1652 | PL_modcount++; /* XXX ??? */ |
| 1653 | break; |
| 1654 | #endif /* USE_5005THREADS */ |
| 1655 | |
| 1656 | case OP_PUSHMARK: |
| 1657 | break; |
| 1658 | |
| 1659 | case OP_KEYS: |
| 1660 | if (type != OP_SASSIGN) |
| 1661 | goto nomod; |
| 1662 | goto lvalue_func; |
| 1663 | case OP_SUBSTR: |
| 1664 | if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */ |
| 1665 | goto nomod; |
| 1666 | /* FALL THROUGH */ |
| 1667 | case OP_POS: |
| 1668 | case OP_VEC: |
| 1669 | if (type == OP_LEAVESUBLV) |
| 1670 | o->op_private |= OPpMAYBE_LVSUB; |
| 1671 | lvalue_func: |
| 1672 | pad_free(o->op_targ); |
| 1673 | o->op_targ = pad_alloc(o->op_type, SVs_PADMY); |
| 1674 | assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL); |
| 1675 | if (o->op_flags & OPf_KIDS) |
| 1676 | mod(cBINOPo->op_first->op_sibling, type); |
| 1677 | break; |
| 1678 | |
| 1679 | case OP_AELEM: |
| 1680 | case OP_HELEM: |
| 1681 | ref(cBINOPo->op_first, o->op_type); |
| 1682 | if (type == OP_ENTERSUB && |
| 1683 | !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) |
| 1684 | o->op_private |= OPpLVAL_DEFER; |
| 1685 | if (type == OP_LEAVESUBLV) |
| 1686 | o->op_private |= OPpMAYBE_LVSUB; |
| 1687 | PL_modcount++; |
| 1688 | break; |
| 1689 | |
| 1690 | case OP_SCOPE: |
| 1691 | case OP_LEAVE: |
| 1692 | case OP_ENTER: |
| 1693 | case OP_LINESEQ: |
| 1694 | if (o->op_flags & OPf_KIDS) |
| 1695 | mod(cLISTOPo->op_last, type); |
| 1696 | break; |
| 1697 | |
| 1698 | case OP_NULL: |
| 1699 | if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ |
| 1700 | goto nomod; |
| 1701 | else if (!(o->op_flags & OPf_KIDS)) |
| 1702 | break; |
| 1703 | if (o->op_targ != OP_LIST) { |
| 1704 | mod(cBINOPo->op_first, type); |
| 1705 | break; |
| 1706 | } |
| 1707 | /* FALL THROUGH */ |
| 1708 | case OP_LIST: |
| 1709 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 1710 | mod(kid, type); |
| 1711 | break; |
| 1712 | |
| 1713 | case OP_RETURN: |
| 1714 | if (type != OP_LEAVESUBLV) |
| 1715 | goto nomod; |
| 1716 | break; /* mod()ing was handled by ck_return() */ |
| 1717 | } |
| 1718 | |
| 1719 | /* [20011101.069] File test operators interpret OPf_REF to mean that |
| 1720 | their argument is a filehandle; thus \stat(".") should not set |
| 1721 | it. AMS 20011102 */ |
| 1722 | if (type == OP_REFGEN && |
| 1723 | PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst)) |
| 1724 | return o; |
| 1725 | |
| 1726 | if (type != OP_LEAVESUBLV) |
| 1727 | o->op_flags |= OPf_MOD; |
| 1728 | |
| 1729 | if (type == OP_AASSIGN || type == OP_SASSIGN) |
| 1730 | o->op_flags |= OPf_SPECIAL|OPf_REF; |
| 1731 | else if (!type) { |
| 1732 | o->op_private |= OPpLVAL_INTRO; |
| 1733 | o->op_flags &= ~OPf_SPECIAL; |
| 1734 | PL_hints |= HINT_BLOCK_SCOPE; |
| 1735 | } |
| 1736 | else if (type != OP_GREPSTART && type != OP_ENTERSUB |
| 1737 | && type != OP_LEAVESUBLV) |
| 1738 | o->op_flags |= OPf_REF; |
| 1739 | return o; |
| 1740 | } |
| 1741 | |
| 1742 | STATIC bool |
| 1743 | S_scalar_mod_type(pTHX_ OP *o, I32 type) |
| 1744 | { |
| 1745 | switch (type) { |
| 1746 | case OP_SASSIGN: |
| 1747 | if (o->op_type == OP_RV2GV) |
| 1748 | return FALSE; |
| 1749 | /* FALL THROUGH */ |
| 1750 | case OP_PREINC: |
| 1751 | case OP_PREDEC: |
| 1752 | case OP_POSTINC: |
| 1753 | case OP_POSTDEC: |
| 1754 | case OP_I_PREINC: |
| 1755 | case OP_I_PREDEC: |
| 1756 | case OP_I_POSTINC: |
| 1757 | case OP_I_POSTDEC: |
| 1758 | case OP_POW: |
| 1759 | case OP_MULTIPLY: |
| 1760 | case OP_DIVIDE: |
| 1761 | case OP_MODULO: |
| 1762 | case OP_REPEAT: |
| 1763 | case OP_ADD: |
| 1764 | case OP_SUBTRACT: |
| 1765 | case OP_I_MULTIPLY: |
| 1766 | case OP_I_DIVIDE: |
| 1767 | case OP_I_MODULO: |
| 1768 | case OP_I_ADD: |
| 1769 | case OP_I_SUBTRACT: |
| 1770 | case OP_LEFT_SHIFT: |
| 1771 | case OP_RIGHT_SHIFT: |
| 1772 | case OP_BIT_AND: |
| 1773 | case OP_BIT_XOR: |
| 1774 | case OP_BIT_OR: |
| 1775 | case OP_CONCAT: |
| 1776 | case OP_SUBST: |
| 1777 | case OP_TRANS: |
| 1778 | case OP_READ: |
| 1779 | case OP_SYSREAD: |
| 1780 | case OP_RECV: |
| 1781 | case OP_ANDASSIGN: |
| 1782 | case OP_ORASSIGN: |
| 1783 | return TRUE; |
| 1784 | default: |
| 1785 | return FALSE; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | STATIC bool |
| 1790 | S_is_handle_constructor(pTHX_ OP *o, I32 argnum) |
| 1791 | { |
| 1792 | switch (o->op_type) { |
| 1793 | case OP_PIPE_OP: |
| 1794 | case OP_SOCKPAIR: |
| 1795 | if (argnum == 2) |
| 1796 | return TRUE; |
| 1797 | /* FALL THROUGH */ |
| 1798 | case OP_SYSOPEN: |
| 1799 | case OP_OPEN: |
| 1800 | case OP_SELECT: /* XXX c.f. SelectSaver.pm */ |
| 1801 | case OP_SOCKET: |
| 1802 | case OP_OPEN_DIR: |
| 1803 | case OP_ACCEPT: |
| 1804 | if (argnum == 1) |
| 1805 | return TRUE; |
| 1806 | /* FALL THROUGH */ |
| 1807 | default: |
| 1808 | return FALSE; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | OP * |
| 1813 | Perl_refkids(pTHX_ OP *o, I32 type) |
| 1814 | { |
| 1815 | OP *kid; |
| 1816 | if (o && o->op_flags & OPf_KIDS) { |
| 1817 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 1818 | ref(kid, type); |
| 1819 | } |
| 1820 | return o; |
| 1821 | } |
| 1822 | |
| 1823 | OP * |
| 1824 | Perl_ref(pTHX_ OP *o, I32 type) |
| 1825 | { |
| 1826 | OP *kid; |
| 1827 | |
| 1828 | if (!o || PL_error_count) |
| 1829 | return o; |
| 1830 | |
| 1831 | switch (o->op_type) { |
| 1832 | case OP_ENTERSUB: |
| 1833 | if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) && |
| 1834 | !(o->op_flags & OPf_STACKED)) { |
| 1835 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ |
| 1836 | o->op_ppaddr = PL_ppaddr[OP_RV2CV]; |
| 1837 | assert(cUNOPo->op_first->op_type == OP_NULL); |
| 1838 | op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ |
| 1839 | o->op_flags |= OPf_SPECIAL; |
| 1840 | } |
| 1841 | break; |
| 1842 | |
| 1843 | case OP_COND_EXPR: |
| 1844 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 1845 | ref(kid, type); |
| 1846 | break; |
| 1847 | case OP_RV2SV: |
| 1848 | if (type == OP_DEFINED) |
| 1849 | o->op_flags |= OPf_SPECIAL; /* don't create GV */ |
| 1850 | ref(cUNOPo->op_first, o->op_type); |
| 1851 | /* FALL THROUGH */ |
| 1852 | case OP_PADSV: |
| 1853 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
| 1854 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
| 1855 | : type == OP_RV2HV ? OPpDEREF_HV |
| 1856 | : OPpDEREF_SV); |
| 1857 | o->op_flags |= OPf_MOD; |
| 1858 | } |
| 1859 | break; |
| 1860 | |
| 1861 | case OP_THREADSV: |
| 1862 | o->op_flags |= OPf_MOD; /* XXX ??? */ |
| 1863 | break; |
| 1864 | |
| 1865 | case OP_RV2AV: |
| 1866 | case OP_RV2HV: |
| 1867 | o->op_flags |= OPf_REF; |
| 1868 | /* FALL THROUGH */ |
| 1869 | case OP_RV2GV: |
| 1870 | if (type == OP_DEFINED) |
| 1871 | o->op_flags |= OPf_SPECIAL; /* don't create GV */ |
| 1872 | ref(cUNOPo->op_first, o->op_type); |
| 1873 | break; |
| 1874 | |
| 1875 | case OP_PADAV: |
| 1876 | case OP_PADHV: |
| 1877 | o->op_flags |= OPf_REF; |
| 1878 | break; |
| 1879 | |
| 1880 | case OP_SCALAR: |
| 1881 | case OP_NULL: |
| 1882 | if (!(o->op_flags & OPf_KIDS)) |
| 1883 | break; |
| 1884 | ref(cBINOPo->op_first, type); |
| 1885 | break; |
| 1886 | case OP_AELEM: |
| 1887 | case OP_HELEM: |
| 1888 | ref(cBINOPo->op_first, o->op_type); |
| 1889 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
| 1890 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
| 1891 | : type == OP_RV2HV ? OPpDEREF_HV |
| 1892 | : OPpDEREF_SV); |
| 1893 | o->op_flags |= OPf_MOD; |
| 1894 | } |
| 1895 | break; |
| 1896 | |
| 1897 | case OP_SCOPE: |
| 1898 | case OP_LEAVE: |
| 1899 | case OP_ENTER: |
| 1900 | case OP_LIST: |
| 1901 | if (!(o->op_flags & OPf_KIDS)) |
| 1902 | break; |
| 1903 | ref(cLISTOPo->op_last, type); |
| 1904 | break; |
| 1905 | default: |
| 1906 | break; |
| 1907 | } |
| 1908 | return scalar(o); |
| 1909 | |
| 1910 | } |
| 1911 | |
| 1912 | STATIC OP * |
| 1913 | S_dup_attrlist(pTHX_ OP *o) |
| 1914 | { |
| 1915 | OP *rop = Nullop; |
| 1916 | |
| 1917 | /* An attrlist is either a simple OP_CONST or an OP_LIST with kids, |
| 1918 | * where the first kid is OP_PUSHMARK and the remaining ones |
| 1919 | * are OP_CONST. We need to push the OP_CONST values. |
| 1920 | */ |
| 1921 | if (o->op_type == OP_CONST) |
| 1922 | rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv)); |
| 1923 | else { |
| 1924 | assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS)); |
| 1925 | for (o = cLISTOPo->op_first; o; o=o->op_sibling) { |
| 1926 | if (o->op_type == OP_CONST) |
| 1927 | rop = append_elem(OP_LIST, rop, |
| 1928 | newSVOP(OP_CONST, o->op_flags, |
| 1929 | SvREFCNT_inc(cSVOPo->op_sv))); |
| 1930 | } |
| 1931 | } |
| 1932 | return rop; |
| 1933 | } |
| 1934 | |
| 1935 | STATIC void |
| 1936 | S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my) |
| 1937 | { |
| 1938 | SV *stashsv; |
| 1939 | |
| 1940 | /* fake up C<use attributes $pkg,$rv,@attrs> */ |
| 1941 | ENTER; /* need to protect against side-effects of 'use' */ |
| 1942 | SAVEINT(PL_expect); |
| 1943 | if (stash) |
| 1944 | stashsv = newSVpv(HvNAME(stash), 0); |
| 1945 | else |
| 1946 | stashsv = &PL_sv_no; |
| 1947 | |
| 1948 | #define ATTRSMODULE "attributes" |
| 1949 | #define ATTRSMODULE_PM "attributes.pm" |
| 1950 | |
| 1951 | if (for_my) { |
| 1952 | SV **svp; |
| 1953 | /* Don't force the C<use> if we don't need it. */ |
| 1954 | svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM, |
| 1955 | sizeof(ATTRSMODULE_PM)-1, 0); |
| 1956 | if (svp && *svp != &PL_sv_undef) |
| 1957 | ; /* already in %INC */ |
| 1958 | else |
| 1959 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, |
| 1960 | newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1), |
| 1961 | Nullsv); |
| 1962 | } |
| 1963 | else { |
| 1964 | Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, |
| 1965 | newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1), |
| 1966 | Nullsv, |
| 1967 | prepend_elem(OP_LIST, |
| 1968 | newSVOP(OP_CONST, 0, stashsv), |
| 1969 | prepend_elem(OP_LIST, |
| 1970 | newSVOP(OP_CONST, 0, |
| 1971 | newRV(target)), |
| 1972 | dup_attrlist(attrs)))); |
| 1973 | } |
| 1974 | LEAVE; |
| 1975 | } |
| 1976 | |
| 1977 | STATIC void |
| 1978 | S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp) |
| 1979 | { |
| 1980 | OP *pack, *imop, *arg; |
| 1981 | SV *meth, *stashsv; |
| 1982 | |
| 1983 | if (!attrs) |
| 1984 | return; |
| 1985 | |
| 1986 | assert(target->op_type == OP_PADSV || |
| 1987 | target->op_type == OP_PADHV || |
| 1988 | target->op_type == OP_PADAV); |
| 1989 | |
| 1990 | /* Ensure that attributes.pm is loaded. */ |
| 1991 | apply_attrs(stash, pad_sv(target->op_targ), attrs, TRUE); |
| 1992 | |
| 1993 | /* Need package name for method call. */ |
| 1994 | pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1)); |
| 1995 | |
| 1996 | /* Build up the real arg-list. */ |
| 1997 | if (stash) |
| 1998 | stashsv = newSVpv(HvNAME(stash), 0); |
| 1999 | else |
| 2000 | stashsv = &PL_sv_no; |
| 2001 | arg = newOP(OP_PADSV, 0); |
| 2002 | arg->op_targ = target->op_targ; |
| 2003 | arg = prepend_elem(OP_LIST, |
| 2004 | newSVOP(OP_CONST, 0, stashsv), |
| 2005 | prepend_elem(OP_LIST, |
| 2006 | newUNOP(OP_REFGEN, 0, |
| 2007 | mod(arg, OP_REFGEN)), |
| 2008 | dup_attrlist(attrs))); |
| 2009 | |
| 2010 | /* Fake up a method call to import */ |
| 2011 | meth = newSVpvn("import", 6); |
| 2012 | (void)SvUPGRADE(meth, SVt_PVIV); |
| 2013 | (void)SvIOK_on(meth); |
| 2014 | PERL_HASH(SvUVX(meth), (U8*)SvPVX(meth), SvCUR(meth)); |
| 2015 | imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID, |
| 2016 | append_elem(OP_LIST, |
| 2017 | prepend_elem(OP_LIST, pack, list(arg)), |
| 2018 | newSVOP(OP_METHOD_NAMED, 0, meth))); |
| 2019 | imop->op_private |= OPpENTERSUB_NOMOD; |
| 2020 | |
| 2021 | /* Combine the ops. */ |
| 2022 | *imopsp = append_elem(OP_LIST, *imopsp, imop); |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | =notfor apidoc apply_attrs_string |
| 2027 | |
| 2028 | Attempts to apply a list of attributes specified by the C<attrstr> and |
| 2029 | C<len> arguments to the subroutine identified by the C<cv> argument which |
| 2030 | is expected to be associated with the package identified by the C<stashpv> |
| 2031 | argument (see L<attributes>). It gets this wrong, though, in that it |
| 2032 | does not correctly identify the boundaries of the individual attribute |
| 2033 | specifications within C<attrstr>. This is not really intended for the |
| 2034 | public API, but has to be listed here for systems such as AIX which |
| 2035 | need an explicit export list for symbols. (It's called from XS code |
| 2036 | in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it |
| 2037 | to respect attribute syntax properly would be welcome. |
| 2038 | |
| 2039 | =cut |
| 2040 | */ |
| 2041 | |
| 2042 | void |
| 2043 | Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv, |
| 2044 | char *attrstr, STRLEN len) |
| 2045 | { |
| 2046 | OP *attrs = Nullop; |
| 2047 | |
| 2048 | if (!len) { |
| 2049 | len = strlen(attrstr); |
| 2050 | } |
| 2051 | |
| 2052 | while (len) { |
| 2053 | for (; isSPACE(*attrstr) && len; --len, ++attrstr) ; |
| 2054 | if (len) { |
| 2055 | char *sstr = attrstr; |
| 2056 | for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ; |
| 2057 | attrs = append_elem(OP_LIST, attrs, |
| 2058 | newSVOP(OP_CONST, 0, |
| 2059 | newSVpvn(sstr, attrstr-sstr))); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, |
| 2064 | newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1), |
| 2065 | Nullsv, prepend_elem(OP_LIST, |
| 2066 | newSVOP(OP_CONST, 0, newSVpv(stashpv,0)), |
| 2067 | prepend_elem(OP_LIST, |
| 2068 | newSVOP(OP_CONST, 0, |
| 2069 | newRV((SV*)cv)), |
| 2070 | attrs))); |
| 2071 | } |
| 2072 | |
| 2073 | STATIC OP * |
| 2074 | S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp) |
| 2075 | { |
| 2076 | OP *kid; |
| 2077 | I32 type; |
| 2078 | |
| 2079 | if (!o || PL_error_count) |
| 2080 | return o; |
| 2081 | |
| 2082 | type = o->op_type; |
| 2083 | if (type == OP_LIST) { |
| 2084 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
| 2085 | my_kid(kid, attrs, imopsp); |
| 2086 | } else if (type == OP_UNDEF) { |
| 2087 | return o; |
| 2088 | } else if (type == OP_RV2SV || /* "our" declaration */ |
| 2089 | type == OP_RV2AV || |
| 2090 | type == OP_RV2HV) { /* XXX does this let anything illegal in? */ |
| 2091 | if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */ |
| 2092 | yyerror(Perl_form(aTHX_ "Can't declare %s in my", OP_DESC(o))); |
| 2093 | } |
| 2094 | if (attrs) { |
| 2095 | GV *gv = cGVOPx_gv(cUNOPo->op_first); |
| 2096 | PL_in_my = FALSE; |
| 2097 | PL_in_my_stash = Nullhv; |
| 2098 | apply_attrs(GvSTASH(gv), |
| 2099 | (type == OP_RV2SV ? GvSV(gv) : |
| 2100 | type == OP_RV2AV ? (SV*)GvAV(gv) : |
| 2101 | type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv), |
| 2102 | attrs, FALSE); |
| 2103 | } |
| 2104 | o->op_private |= OPpOUR_INTRO; |
| 2105 | return o; |
| 2106 | } |
| 2107 | else if (type != OP_PADSV && |
| 2108 | type != OP_PADAV && |
| 2109 | type != OP_PADHV && |
| 2110 | type != OP_PUSHMARK) |
| 2111 | { |
| 2112 | yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", |
| 2113 | OP_DESC(o), |
| 2114 | PL_in_my == KEY_our ? "our" : "my")); |
| 2115 | return o; |
| 2116 | } |
| 2117 | else if (attrs && type != OP_PUSHMARK) { |
| 2118 | HV *stash; |
| 2119 | SV **namesvp; |
| 2120 | |
| 2121 | PL_in_my = FALSE; |
| 2122 | PL_in_my_stash = Nullhv; |
| 2123 | |
| 2124 | /* check for C<my Dog $spot> when deciding package */ |
| 2125 | namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE); |
| 2126 | if (namesvp && *namesvp && (SvFLAGS(*namesvp) & SVpad_TYPED)) |
| 2127 | stash = SvSTASH(*namesvp); |
| 2128 | else |
| 2129 | stash = PL_curstash; |
| 2130 | apply_attrs_my(stash, o, attrs, imopsp); |
| 2131 | } |
| 2132 | o->op_flags |= OPf_MOD; |
| 2133 | o->op_private |= OPpLVAL_INTRO; |
| 2134 | return o; |
| 2135 | } |
| 2136 | |
| 2137 | OP * |
| 2138 | Perl_my_attrs(pTHX_ OP *o, OP *attrs) |
| 2139 | { |
| 2140 | OP *rops = Nullop; |
| 2141 | int maybe_scalar = 0; |
| 2142 | |
| 2143 | if (o->op_flags & OPf_PARENS) |
| 2144 | list(o); |
| 2145 | else |
| 2146 | maybe_scalar = 1; |
| 2147 | if (attrs) |
| 2148 | SAVEFREEOP(attrs); |
| 2149 | o = my_kid(o, attrs, &rops); |
| 2150 | if (rops) { |
| 2151 | if (maybe_scalar && o->op_type == OP_PADSV) { |
| 2152 | o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o)); |
| 2153 | o->op_private |= OPpLVAL_INTRO; |
| 2154 | } |
| 2155 | else |
| 2156 | o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops); |
| 2157 | } |
| 2158 | PL_in_my = FALSE; |
| 2159 | PL_in_my_stash = Nullhv; |
| 2160 | return o; |
| 2161 | } |
| 2162 | |
| 2163 | OP * |
| 2164 | Perl_my(pTHX_ OP *o) |
| 2165 | { |
| 2166 | return my_attrs(o, Nullop); |
| 2167 | } |
| 2168 | |
| 2169 | OP * |
| 2170 | Perl_sawparens(pTHX_ OP *o) |
| 2171 | { |
| 2172 | if (o) |
| 2173 | o->op_flags |= OPf_PARENS; |
| 2174 | return o; |
| 2175 | } |
| 2176 | |
| 2177 | OP * |
| 2178 | Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) |
| 2179 | { |
| 2180 | OP *o; |
| 2181 | |
| 2182 | if (ckWARN(WARN_MISC) && |
| 2183 | (left->op_type == OP_RV2AV || |
| 2184 | left->op_type == OP_RV2HV || |
| 2185 | left->op_type == OP_PADAV || |
| 2186 | left->op_type == OP_PADHV)) { |
| 2187 | char *desc = PL_op_desc[(right->op_type == OP_SUBST || |
| 2188 | right->op_type == OP_TRANS) |
| 2189 | ? right->op_type : OP_MATCH]; |
| 2190 | const char *sample = ((left->op_type == OP_RV2AV || |
| 2191 | left->op_type == OP_PADAV) |
| 2192 | ? "@array" : "%hash"); |
| 2193 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 2194 | "Applying %s to %s will act on scalar(%s)", |
| 2195 | desc, sample, sample); |
| 2196 | } |
| 2197 | |
| 2198 | if (right->op_type == OP_CONST && |
| 2199 | cSVOPx(right)->op_private & OPpCONST_BARE && |
| 2200 | cSVOPx(right)->op_private & OPpCONST_STRICT) |
| 2201 | { |
| 2202 | no_bareword_allowed(right); |
| 2203 | } |
| 2204 | |
| 2205 | if (!(right->op_flags & OPf_STACKED) && |
| 2206 | (right->op_type == OP_MATCH || |
| 2207 | right->op_type == OP_SUBST || |
| 2208 | right->op_type == OP_TRANS)) { |
| 2209 | right->op_flags |= OPf_STACKED; |
| 2210 | if (right->op_type != OP_MATCH && |
| 2211 | ! (right->op_type == OP_TRANS && |
| 2212 | right->op_private & OPpTRANS_IDENTICAL)) |
| 2213 | left = mod(left, right->op_type); |
| 2214 | if (right->op_type == OP_TRANS) |
| 2215 | o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); |
| 2216 | else |
| 2217 | o = prepend_elem(right->op_type, scalar(left), right); |
| 2218 | if (type == OP_NOT) |
| 2219 | return newUNOP(OP_NOT, 0, scalar(o)); |
| 2220 | return o; |
| 2221 | } |
| 2222 | else |
| 2223 | return bind_match(type, left, |
| 2224 | pmruntime(newPMOP(OP_MATCH, 0), right, Nullop)); |
| 2225 | } |
| 2226 | |
| 2227 | OP * |
| 2228 | Perl_invert(pTHX_ OP *o) |
| 2229 | { |
| 2230 | if (!o) |
| 2231 | return o; |
| 2232 | /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */ |
| 2233 | return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); |
| 2234 | } |
| 2235 | |
| 2236 | OP * |
| 2237 | Perl_scope(pTHX_ OP *o) |
| 2238 | { |
| 2239 | if (o) { |
| 2240 | if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) { |
| 2241 | o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); |
| 2242 | o->op_type = OP_LEAVE; |
| 2243 | o->op_ppaddr = PL_ppaddr[OP_LEAVE]; |
| 2244 | } |
| 2245 | else { |
| 2246 | if (o->op_type == OP_LINESEQ) { |
| 2247 | OP *kid; |
| 2248 | o->op_type = OP_SCOPE; |
| 2249 | o->op_ppaddr = PL_ppaddr[OP_SCOPE]; |
| 2250 | kid = ((LISTOP*)o)->op_first; |
| 2251 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) |
| 2252 | op_null(kid); |
| 2253 | } |
| 2254 | else |
| 2255 | o = newLISTOP(OP_SCOPE, 0, o, Nullop); |
| 2256 | } |
| 2257 | } |
| 2258 | return o; |
| 2259 | } |
| 2260 | |
| 2261 | void |
| 2262 | Perl_save_hints(pTHX) |
| 2263 | { |
| 2264 | SAVEI32(PL_hints); |
| 2265 | SAVESPTR(GvHV(PL_hintgv)); |
| 2266 | GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv)); |
| 2267 | SAVEFREESV(GvHV(PL_hintgv)); |
| 2268 | } |
| 2269 | |
| 2270 | int |
| 2271 | Perl_block_start(pTHX_ int full) |
| 2272 | { |
| 2273 | int retval = PL_savestack_ix; |
| 2274 | |
| 2275 | SAVEI32(PL_comppad_name_floor); |
| 2276 | PL_comppad_name_floor = AvFILLp(PL_comppad_name); |
| 2277 | if (full) |
| 2278 | PL_comppad_name_fill = PL_comppad_name_floor; |
| 2279 | if (PL_comppad_name_floor < 0) |
| 2280 | PL_comppad_name_floor = 0; |
| 2281 | SAVEI32(PL_min_intro_pending); |
| 2282 | SAVEI32(PL_max_intro_pending); |
| 2283 | PL_min_intro_pending = 0; |
| 2284 | SAVEI32(PL_comppad_name_fill); |
| 2285 | SAVEI32(PL_padix_floor); |
| 2286 | PL_padix_floor = PL_padix; |
| 2287 | PL_pad_reset_pending = FALSE; |
| 2288 | SAVEHINTS(); |
| 2289 | PL_hints &= ~HINT_BLOCK_SCOPE; |
| 2290 | SAVESPTR(PL_compiling.cop_warnings); |
| 2291 | if (! specialWARN(PL_compiling.cop_warnings)) { |
| 2292 | PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ; |
| 2293 | SAVEFREESV(PL_compiling.cop_warnings) ; |
| 2294 | } |
| 2295 | SAVESPTR(PL_compiling.cop_io); |
| 2296 | if (! specialCopIO(PL_compiling.cop_io)) { |
| 2297 | PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ; |
| 2298 | SAVEFREESV(PL_compiling.cop_io) ; |
| 2299 | } |
| 2300 | return retval; |
| 2301 | } |
| 2302 | |
| 2303 | OP* |
| 2304 | Perl_block_end(pTHX_ I32 floor, OP *seq) |
| 2305 | { |
| 2306 | int needblockscope = PL_hints & HINT_BLOCK_SCOPE; |
| 2307 | line_t copline = PL_copline; |
| 2308 | /* there should be a nextstate in every block */ |
| 2309 | OP* retval = seq ? scalarseq(seq) : newSTATEOP(0, Nullch, seq); |
| 2310 | PL_copline = copline; /* XXX newSTATEOP may reset PL_copline */ |
| 2311 | LEAVE_SCOPE(floor); |
| 2312 | PL_pad_reset_pending = FALSE; |
| 2313 | PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK); |
| 2314 | if (needblockscope) |
| 2315 | PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */ |
| 2316 | pad_leavemy(PL_comppad_name_fill); |
| 2317 | PL_cop_seqmax++; |
| 2318 | return retval; |
| 2319 | } |
| 2320 | |
| 2321 | STATIC OP * |
| 2322 | S_newDEFSVOP(pTHX) |
| 2323 | { |
| 2324 | #ifdef USE_5005THREADS |
| 2325 | OP *o = newOP(OP_THREADSV, 0); |
| 2326 | o->op_targ = find_threadsv("_"); |
| 2327 | return o; |
| 2328 | #else |
| 2329 | return newSVREF(newGVOP(OP_GV, 0, PL_defgv)); |
| 2330 | #endif /* USE_5005THREADS */ |
| 2331 | } |
| 2332 | |
| 2333 | void |
| 2334 | Perl_newPROG(pTHX_ OP *o) |
| 2335 | { |
| 2336 | if (PL_in_eval) { |
| 2337 | if (PL_eval_root) |
| 2338 | return; |
| 2339 | PL_eval_root = newUNOP(OP_LEAVEEVAL, |
| 2340 | ((PL_in_eval & EVAL_KEEPERR) |
| 2341 | ? OPf_SPECIAL : 0), o); |
| 2342 | PL_eval_start = linklist(PL_eval_root); |
| 2343 | PL_eval_root->op_private |= OPpREFCOUNTED; |
| 2344 | OpREFCNT_set(PL_eval_root, 1); |
| 2345 | PL_eval_root->op_next = 0; |
| 2346 | CALL_PEEP(PL_eval_start); |
| 2347 | } |
| 2348 | else { |
| 2349 | if (!o) |
| 2350 | return; |
| 2351 | PL_main_root = scope(sawparens(scalarvoid(o))); |
| 2352 | PL_curcop = &PL_compiling; |
| 2353 | PL_main_start = LINKLIST(PL_main_root); |
| 2354 | PL_main_root->op_private |= OPpREFCOUNTED; |
| 2355 | OpREFCNT_set(PL_main_root, 1); |
| 2356 | PL_main_root->op_next = 0; |
| 2357 | CALL_PEEP(PL_main_start); |
| 2358 | PL_compcv = 0; |
| 2359 | |
| 2360 | /* Register with debugger */ |
| 2361 | if (PERLDB_INTER) { |
| 2362 | CV *cv = get_cv("DB::postponed", FALSE); |
| 2363 | if (cv) { |
| 2364 | dSP; |
| 2365 | PUSHMARK(SP); |
| 2366 | XPUSHs((SV*)CopFILEGV(&PL_compiling)); |
| 2367 | PUTBACK; |
| 2368 | call_sv((SV*)cv, G_DISCARD); |
| 2369 | } |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | OP * |
| 2375 | Perl_localize(pTHX_ OP *o, I32 lex) |
| 2376 | { |
| 2377 | if (o->op_flags & OPf_PARENS) |
| 2378 | list(o); |
| 2379 | else { |
| 2380 | if (ckWARN(WARN_PARENTHESIS) |
| 2381 | && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') |
| 2382 | { |
| 2383 | char *s = PL_bufptr; |
| 2384 | |
| 2385 | while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s) || strchr("@$%, ", *s))) |
| 2386 | s++; |
| 2387 | |
| 2388 | if (*s == ';' || *s == '=') |
| 2389 | Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS), |
| 2390 | "Parentheses missing around \"%s\" list", |
| 2391 | lex ? (PL_in_my == KEY_our ? "our" : "my") : "local"); |
| 2392 | } |
| 2393 | } |
| 2394 | if (lex) |
| 2395 | o = my(o); |
| 2396 | else |
| 2397 | o = mod(o, OP_NULL); /* a bit kludgey */ |
| 2398 | PL_in_my = FALSE; |
| 2399 | PL_in_my_stash = Nullhv; |
| 2400 | return o; |
| 2401 | } |
| 2402 | |
| 2403 | OP * |
| 2404 | Perl_jmaybe(pTHX_ OP *o) |
| 2405 | { |
| 2406 | if (o->op_type == OP_LIST) { |
| 2407 | OP *o2; |
| 2408 | #ifdef USE_5005THREADS |
| 2409 | o2 = newOP(OP_THREADSV, 0); |
| 2410 | o2->op_targ = find_threadsv(";"); |
| 2411 | #else |
| 2412 | o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))), |
| 2413 | #endif /* USE_5005THREADS */ |
| 2414 | o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o)); |
| 2415 | } |
| 2416 | return o; |
| 2417 | } |
| 2418 | |
| 2419 | OP * |
| 2420 | Perl_fold_constants(pTHX_ register OP *o) |
| 2421 | { |
| 2422 | register OP *curop; |
| 2423 | I32 type = o->op_type; |
| 2424 | SV *sv; |
| 2425 | |
| 2426 | if (PL_opargs[type] & OA_RETSCALAR) |
| 2427 | scalar(o); |
| 2428 | if (PL_opargs[type] & OA_TARGET && !o->op_targ) |
| 2429 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
| 2430 | |
| 2431 | /* integerize op, unless it happens to be C<-foo>. |
| 2432 | * XXX should pp_i_negate() do magic string negation instead? */ |
| 2433 | if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER) |
| 2434 | && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST |
| 2435 | && (cUNOPo->op_first->op_private & OPpCONST_BARE))) |
| 2436 | { |
| 2437 | o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)]; |
| 2438 | } |
| 2439 | |
| 2440 | if (!(PL_opargs[type] & OA_FOLDCONST)) |
| 2441 | goto nope; |
| 2442 | |
| 2443 | switch (type) { |
| 2444 | case OP_NEGATE: |
| 2445 | /* XXX might want a ck_negate() for this */ |
| 2446 | cUNOPo->op_first->op_private &= ~OPpCONST_STRICT; |
| 2447 | break; |
| 2448 | case OP_SPRINTF: |
| 2449 | case OP_UCFIRST: |
| 2450 | case OP_LCFIRST: |
| 2451 | case OP_UC: |
| 2452 | case OP_LC: |
| 2453 | case OP_SLT: |
| 2454 | case OP_SGT: |
| 2455 | case OP_SLE: |
| 2456 | case OP_SGE: |
| 2457 | case OP_SCMP: |
| 2458 | /* XXX what about the numeric ops? */ |
| 2459 | if (PL_hints & HINT_LOCALE) |
| 2460 | goto nope; |
| 2461 | } |
| 2462 | |
| 2463 | if (PL_error_count) |
| 2464 | goto nope; /* Don't try to run w/ errors */ |
| 2465 | |
| 2466 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
| 2467 | if ((curop->op_type != OP_CONST || |
| 2468 | (curop->op_private & OPpCONST_BARE)) && |
| 2469 | curop->op_type != OP_LIST && |
| 2470 | curop->op_type != OP_SCALAR && |
| 2471 | curop->op_type != OP_NULL && |
| 2472 | curop->op_type != OP_PUSHMARK) |
| 2473 | { |
| 2474 | goto nope; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | curop = LINKLIST(o); |
| 2479 | o->op_next = 0; |
| 2480 | PL_op = curop; |
| 2481 | CALLRUNOPS(aTHX); |
| 2482 | sv = *(PL_stack_sp--); |
| 2483 | if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */ |
| 2484 | pad_swipe(o->op_targ); |
| 2485 | else if (SvTEMP(sv)) { /* grab mortal temp? */ |
| 2486 | (void)SvREFCNT_inc(sv); |
| 2487 | SvTEMP_off(sv); |
| 2488 | } |
| 2489 | op_free(o); |
| 2490 | if (type == OP_RV2GV) |
| 2491 | return newGVOP(OP_GV, 0, (GV*)sv); |
| 2492 | else { |
| 2493 | /* try to smush double to int, but don't smush -2.0 to -2 */ |
| 2494 | if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK && |
| 2495 | type != OP_NEGATE) |
| 2496 | { |
| 2497 | #ifdef PERL_PRESERVE_IVUV |
| 2498 | /* Only bother to attempt to fold to IV if |
| 2499 | most operators will benefit */ |
| 2500 | SvIV_please(sv); |
| 2501 | #endif |
| 2502 | } |
| 2503 | return newSVOP(OP_CONST, 0, sv); |
| 2504 | } |
| 2505 | |
| 2506 | nope: |
| 2507 | return o; |
| 2508 | } |
| 2509 | |
| 2510 | OP * |
| 2511 | Perl_gen_constant_list(pTHX_ register OP *o) |
| 2512 | { |
| 2513 | register OP *curop; |
| 2514 | I32 oldtmps_floor = PL_tmps_floor; |
| 2515 | |
| 2516 | list(o); |
| 2517 | if (PL_error_count) |
| 2518 | return o; /* Don't attempt to run with errors */ |
| 2519 | |
| 2520 | PL_op = curop = LINKLIST(o); |
| 2521 | o->op_next = 0; |
| 2522 | CALL_PEEP(curop); |
| 2523 | pp_pushmark(); |
| 2524 | CALLRUNOPS(aTHX); |
| 2525 | PL_op = curop; |
| 2526 | pp_anonlist(); |
| 2527 | PL_tmps_floor = oldtmps_floor; |
| 2528 | |
| 2529 | o->op_type = OP_RV2AV; |
| 2530 | o->op_ppaddr = PL_ppaddr[OP_RV2AV]; |
| 2531 | o->op_seq = 0; /* needs to be revisited in peep() */ |
| 2532 | curop = ((UNOP*)o)->op_first; |
| 2533 | ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--)); |
| 2534 | op_free(curop); |
| 2535 | linklist(o); |
| 2536 | return list(o); |
| 2537 | } |
| 2538 | |
| 2539 | OP * |
| 2540 | Perl_convert(pTHX_ I32 type, I32 flags, OP *o) |
| 2541 | { |
| 2542 | if (!o || o->op_type != OP_LIST) |
| 2543 | o = newLISTOP(OP_LIST, 0, o, Nullop); |
| 2544 | else |
| 2545 | o->op_flags &= ~OPf_WANT; |
| 2546 | |
| 2547 | if (!(PL_opargs[type] & OA_MARK)) |
| 2548 | op_null(cLISTOPo->op_first); |
| 2549 | |
| 2550 | o->op_type = (OPCODE)type; |
| 2551 | o->op_ppaddr = PL_ppaddr[type]; |
| 2552 | o->op_flags |= flags; |
| 2553 | |
| 2554 | o = CHECKOP(type, o); |
| 2555 | if (o->op_type != type) |
| 2556 | return o; |
| 2557 | |
| 2558 | return fold_constants(o); |
| 2559 | } |
| 2560 | |
| 2561 | /* List constructors */ |
| 2562 | |
| 2563 | OP * |
| 2564 | Perl_append_elem(pTHX_ I32 type, OP *first, OP *last) |
| 2565 | { |
| 2566 | if (!first) |
| 2567 | return last; |
| 2568 | |
| 2569 | if (!last) |
| 2570 | return first; |
| 2571 | |
| 2572 | if (first->op_type != type |
| 2573 | || (type == OP_LIST && (first->op_flags & OPf_PARENS))) |
| 2574 | { |
| 2575 | return newLISTOP(type, 0, first, last); |
| 2576 | } |
| 2577 | |
| 2578 | if (first->op_flags & OPf_KIDS) |
| 2579 | ((LISTOP*)first)->op_last->op_sibling = last; |
| 2580 | else { |
| 2581 | first->op_flags |= OPf_KIDS; |
| 2582 | ((LISTOP*)first)->op_first = last; |
| 2583 | } |
| 2584 | ((LISTOP*)first)->op_last = last; |
| 2585 | return first; |
| 2586 | } |
| 2587 | |
| 2588 | OP * |
| 2589 | Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last) |
| 2590 | { |
| 2591 | if (!first) |
| 2592 | return (OP*)last; |
| 2593 | |
| 2594 | if (!last) |
| 2595 | return (OP*)first; |
| 2596 | |
| 2597 | if (first->op_type != type) |
| 2598 | return prepend_elem(type, (OP*)first, (OP*)last); |
| 2599 | |
| 2600 | if (last->op_type != type) |
| 2601 | return append_elem(type, (OP*)first, (OP*)last); |
| 2602 | |
| 2603 | first->op_last->op_sibling = last->op_first; |
| 2604 | first->op_last = last->op_last; |
| 2605 | first->op_flags |= (last->op_flags & OPf_KIDS); |
| 2606 | |
| 2607 | FreeOp(last); |
| 2608 | |
| 2609 | return (OP*)first; |
| 2610 | } |
| 2611 | |
| 2612 | OP * |
| 2613 | Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last) |
| 2614 | { |
| 2615 | if (!first) |
| 2616 | return last; |
| 2617 | |
| 2618 | if (!last) |
| 2619 | return first; |
| 2620 | |
| 2621 | if (last->op_type == type) { |
| 2622 | if (type == OP_LIST) { /* already a PUSHMARK there */ |
| 2623 | first->op_sibling = ((LISTOP*)last)->op_first->op_sibling; |
| 2624 | ((LISTOP*)last)->op_first->op_sibling = first; |
| 2625 | if (!(first->op_flags & OPf_PARENS)) |
| 2626 | last->op_flags &= ~OPf_PARENS; |
| 2627 | } |
| 2628 | else { |
| 2629 | if (!(last->op_flags & OPf_KIDS)) { |
| 2630 | ((LISTOP*)last)->op_last = first; |
| 2631 | last->op_flags |= OPf_KIDS; |
| 2632 | } |
| 2633 | first->op_sibling = ((LISTOP*)last)->op_first; |
| 2634 | ((LISTOP*)last)->op_first = first; |
| 2635 | } |
| 2636 | last->op_flags |= OPf_KIDS; |
| 2637 | return last; |
| 2638 | } |
| 2639 | |
| 2640 | return newLISTOP(type, 0, first, last); |
| 2641 | } |
| 2642 | |
| 2643 | /* Constructors */ |
| 2644 | |
| 2645 | OP * |
| 2646 | Perl_newNULLLIST(pTHX) |
| 2647 | { |
| 2648 | return newOP(OP_STUB, 0); |
| 2649 | } |
| 2650 | |
| 2651 | OP * |
| 2652 | Perl_force_list(pTHX_ OP *o) |
| 2653 | { |
| 2654 | if (!o || o->op_type != OP_LIST) |
| 2655 | o = newLISTOP(OP_LIST, 0, o, Nullop); |
| 2656 | op_null(o); |
| 2657 | return o; |
| 2658 | } |
| 2659 | |
| 2660 | OP * |
| 2661 | Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) |
| 2662 | { |
| 2663 | LISTOP *listop; |
| 2664 | |
| 2665 | NewOp(1101, listop, 1, LISTOP); |
| 2666 | |
| 2667 | listop->op_type = (OPCODE)type; |
| 2668 | listop->op_ppaddr = PL_ppaddr[type]; |
| 2669 | if (first || last) |
| 2670 | flags |= OPf_KIDS; |
| 2671 | listop->op_flags = (U8)flags; |
| 2672 | |
| 2673 | if (!last && first) |
| 2674 | last = first; |
| 2675 | else if (!first && last) |
| 2676 | first = last; |
| 2677 | else if (first) |
| 2678 | first->op_sibling = last; |
| 2679 | listop->op_first = first; |
| 2680 | listop->op_last = last; |
| 2681 | if (type == OP_LIST) { |
| 2682 | OP* pushop; |
| 2683 | pushop = newOP(OP_PUSHMARK, 0); |
| 2684 | pushop->op_sibling = first; |
| 2685 | listop->op_first = pushop; |
| 2686 | listop->op_flags |= OPf_KIDS; |
| 2687 | if (!last) |
| 2688 | listop->op_last = pushop; |
| 2689 | } |
| 2690 | |
| 2691 | return (OP*)listop; |
| 2692 | } |
| 2693 | |
| 2694 | OP * |
| 2695 | Perl_newOP(pTHX_ I32 type, I32 flags) |
| 2696 | { |
| 2697 | OP *o; |
| 2698 | NewOp(1101, o, 1, OP); |
| 2699 | o->op_type = (OPCODE)type; |
| 2700 | o->op_ppaddr = PL_ppaddr[type]; |
| 2701 | o->op_flags = (U8)flags; |
| 2702 | |
| 2703 | o->op_next = o; |
| 2704 | o->op_private = (U8)(0 | (flags >> 8)); |
| 2705 | if (PL_opargs[type] & OA_RETSCALAR) |
| 2706 | scalar(o); |
| 2707 | if (PL_opargs[type] & OA_TARGET) |
| 2708 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
| 2709 | return CHECKOP(type, o); |
| 2710 | } |
| 2711 | |
| 2712 | OP * |
| 2713 | Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first) |
| 2714 | { |
| 2715 | UNOP *unop; |
| 2716 | |
| 2717 | if (!first) |
| 2718 | first = newOP(OP_STUB, 0); |
| 2719 | if (PL_opargs[type] & OA_MARK) |
| 2720 | first = force_list(first); |
| 2721 | |
| 2722 | NewOp(1101, unop, 1, UNOP); |
| 2723 | unop->op_type = (OPCODE)type; |
| 2724 | unop->op_ppaddr = PL_ppaddr[type]; |
| 2725 | unop->op_first = first; |
| 2726 | unop->op_flags = flags | OPf_KIDS; |
| 2727 | unop->op_private = (U8)(1 | (flags >> 8)); |
| 2728 | unop = (UNOP*) CHECKOP(type, unop); |
| 2729 | if (unop->op_next) |
| 2730 | return (OP*)unop; |
| 2731 | |
| 2732 | return fold_constants((OP *) unop); |
| 2733 | } |
| 2734 | |
| 2735 | OP * |
| 2736 | Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) |
| 2737 | { |
| 2738 | BINOP *binop; |
| 2739 | NewOp(1101, binop, 1, BINOP); |
| 2740 | |
| 2741 | if (!first) |
| 2742 | first = newOP(OP_NULL, 0); |
| 2743 | |
| 2744 | binop->op_type = (OPCODE)type; |
| 2745 | binop->op_ppaddr = PL_ppaddr[type]; |
| 2746 | binop->op_first = first; |
| 2747 | binop->op_flags = flags | OPf_KIDS; |
| 2748 | if (!last) { |
| 2749 | last = first; |
| 2750 | binop->op_private = (U8)(1 | (flags >> 8)); |
| 2751 | } |
| 2752 | else { |
| 2753 | binop->op_private = (U8)(2 | (flags >> 8)); |
| 2754 | first->op_sibling = last; |
| 2755 | } |
| 2756 | |
| 2757 | binop = (BINOP*)CHECKOP(type, binop); |
| 2758 | if (binop->op_next || binop->op_type != (OPCODE)type) |
| 2759 | return (OP*)binop; |
| 2760 | |
| 2761 | binop->op_last = binop->op_first->op_sibling; |
| 2762 | |
| 2763 | return fold_constants((OP *)binop); |
| 2764 | } |
| 2765 | |
| 2766 | static int |
| 2767 | uvcompare(const void *a, const void *b) |
| 2768 | { |
| 2769 | if (*((UV *)a) < (*(UV *)b)) |
| 2770 | return -1; |
| 2771 | if (*((UV *)a) > (*(UV *)b)) |
| 2772 | return 1; |
| 2773 | if (*((UV *)a+1) < (*(UV *)b+1)) |
| 2774 | return -1; |
| 2775 | if (*((UV *)a+1) > (*(UV *)b+1)) |
| 2776 | return 1; |
| 2777 | return 0; |
| 2778 | } |
| 2779 | |
| 2780 | OP * |
| 2781 | Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) |
| 2782 | { |
| 2783 | SV *tstr = ((SVOP*)expr)->op_sv; |
| 2784 | SV *rstr = ((SVOP*)repl)->op_sv; |
| 2785 | STRLEN tlen; |
| 2786 | STRLEN rlen; |
| 2787 | U8 *t = (U8*)SvPV(tstr, tlen); |
| 2788 | U8 *r = (U8*)SvPV(rstr, rlen); |
| 2789 | register I32 i; |
| 2790 | register I32 j; |
| 2791 | I32 del; |
| 2792 | I32 complement; |
| 2793 | I32 squash; |
| 2794 | I32 grows = 0; |
| 2795 | register short *tbl; |
| 2796 | |
| 2797 | PL_hints |= HINT_BLOCK_SCOPE; |
| 2798 | complement = o->op_private & OPpTRANS_COMPLEMENT; |
| 2799 | del = o->op_private & OPpTRANS_DELETE; |
| 2800 | squash = o->op_private & OPpTRANS_SQUASH; |
| 2801 | |
| 2802 | if (SvUTF8(tstr)) |
| 2803 | o->op_private |= OPpTRANS_FROM_UTF; |
| 2804 | |
| 2805 | if (SvUTF8(rstr)) |
| 2806 | o->op_private |= OPpTRANS_TO_UTF; |
| 2807 | |
| 2808 | if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { |
| 2809 | SV* listsv = newSVpvn("# comment\n",10); |
| 2810 | SV* transv = 0; |
| 2811 | U8* tend = t + tlen; |
| 2812 | U8* rend = r + rlen; |
| 2813 | STRLEN ulen; |
| 2814 | U32 tfirst = 1; |
| 2815 | U32 tlast = 0; |
| 2816 | I32 tdiff; |
| 2817 | U32 rfirst = 1; |
| 2818 | U32 rlast = 0; |
| 2819 | I32 rdiff; |
| 2820 | I32 diff; |
| 2821 | I32 none = 0; |
| 2822 | U32 max = 0; |
| 2823 | I32 bits; |
| 2824 | I32 havefinal = 0; |
| 2825 | U32 final = 0; |
| 2826 | I32 from_utf = o->op_private & OPpTRANS_FROM_UTF; |
| 2827 | I32 to_utf = o->op_private & OPpTRANS_TO_UTF; |
| 2828 | U8* tsave = NULL; |
| 2829 | U8* rsave = NULL; |
| 2830 | |
| 2831 | if (!from_utf) { |
| 2832 | STRLEN len = tlen; |
| 2833 | tsave = t = bytes_to_utf8(t, &len); |
| 2834 | tend = t + len; |
| 2835 | } |
| 2836 | if (!to_utf && rlen) { |
| 2837 | STRLEN len = rlen; |
| 2838 | rsave = r = bytes_to_utf8(r, &len); |
| 2839 | rend = r + len; |
| 2840 | } |
| 2841 | |
| 2842 | /* There are several snags with this code on EBCDIC: |
| 2843 | 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes). |
| 2844 | 2. scan_const() in toke.c has encoded chars in native encoding which makes |
| 2845 | ranges at least in EBCDIC 0..255 range the bottom odd. |
| 2846 | */ |
| 2847 | |
| 2848 | if (complement) { |
| 2849 | U8 tmpbuf[UTF8_MAXLEN+1]; |
| 2850 | UV *cp; |
| 2851 | UV nextmin = 0; |
| 2852 | New(1109, cp, 2*tlen, UV); |
| 2853 | i = 0; |
| 2854 | transv = newSVpvn("",0); |
| 2855 | while (t < tend) { |
| 2856 | cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0); |
| 2857 | t += ulen; |
| 2858 | if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { |
| 2859 | t++; |
| 2860 | cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0); |
| 2861 | t += ulen; |
| 2862 | } |
| 2863 | else { |
| 2864 | cp[2*i+1] = cp[2*i]; |
| 2865 | } |
| 2866 | i++; |
| 2867 | } |
| 2868 | qsort(cp, i, 2*sizeof(UV), uvcompare); |
| 2869 | for (j = 0; j < i; j++) { |
| 2870 | UV val = cp[2*j]; |
| 2871 | diff = val - nextmin; |
| 2872 | if (diff > 0) { |
| 2873 | t = uvuni_to_utf8(tmpbuf,nextmin); |
| 2874 | sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); |
| 2875 | if (diff > 1) { |
| 2876 | U8 range_mark = UTF_TO_NATIVE(0xff); |
| 2877 | t = uvuni_to_utf8(tmpbuf, val - 1); |
| 2878 | sv_catpvn(transv, (char *)&range_mark, 1); |
| 2879 | sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); |
| 2880 | } |
| 2881 | } |
| 2882 | val = cp[2*j+1]; |
| 2883 | if (val >= nextmin) |
| 2884 | nextmin = val + 1; |
| 2885 | } |
| 2886 | t = uvuni_to_utf8(tmpbuf,nextmin); |
| 2887 | sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); |
| 2888 | { |
| 2889 | U8 range_mark = UTF_TO_NATIVE(0xff); |
| 2890 | sv_catpvn(transv, (char *)&range_mark, 1); |
| 2891 | } |
| 2892 | t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff, |
| 2893 | UNICODE_ALLOW_SUPER); |
| 2894 | sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); |
| 2895 | t = (U8*)SvPVX(transv); |
| 2896 | tlen = SvCUR(transv); |
| 2897 | tend = t + tlen; |
| 2898 | Safefree(cp); |
| 2899 | } |
| 2900 | else if (!rlen && !del) { |
| 2901 | r = t; rlen = tlen; rend = tend; |
| 2902 | } |
| 2903 | if (!squash) { |
| 2904 | if ((!rlen && !del) || t == r || |
| 2905 | (tlen == rlen && memEQ((char *)t, (char *)r, tlen))) |
| 2906 | { |
| 2907 | o->op_private |= OPpTRANS_IDENTICAL; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | while (t < tend || tfirst <= tlast) { |
| 2912 | /* see if we need more "t" chars */ |
| 2913 | if (tfirst > tlast) { |
| 2914 | tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0); |
| 2915 | t += ulen; |
| 2916 | if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */ |
| 2917 | t++; |
| 2918 | tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0); |
| 2919 | t += ulen; |
| 2920 | } |
| 2921 | else |
| 2922 | tlast = tfirst; |
| 2923 | } |
| 2924 | |
| 2925 | /* now see if we need more "r" chars */ |
| 2926 | if (rfirst > rlast) { |
| 2927 | if (r < rend) { |
| 2928 | rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0); |
| 2929 | r += ulen; |
| 2930 | if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */ |
| 2931 | r++; |
| 2932 | rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0); |
| 2933 | r += ulen; |
| 2934 | } |
| 2935 | else |
| 2936 | rlast = rfirst; |
| 2937 | } |
| 2938 | else { |
| 2939 | if (!havefinal++) |
| 2940 | final = rlast; |
| 2941 | rfirst = rlast = 0xffffffff; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | /* now see which range will peter our first, if either. */ |
| 2946 | tdiff = tlast - tfirst; |
| 2947 | rdiff = rlast - rfirst; |
| 2948 | |
| 2949 | if (tdiff <= rdiff) |
| 2950 | diff = tdiff; |
| 2951 | else |
| 2952 | diff = rdiff; |
| 2953 | |
| 2954 | if (rfirst == 0xffffffff) { |
| 2955 | diff = tdiff; /* oops, pretend rdiff is infinite */ |
| 2956 | if (diff > 0) |
| 2957 | Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n", |
| 2958 | (long)tfirst, (long)tlast); |
| 2959 | else |
| 2960 | Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst); |
| 2961 | } |
| 2962 | else { |
| 2963 | if (diff > 0) |
| 2964 | Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n", |
| 2965 | (long)tfirst, (long)(tfirst + diff), |
| 2966 | (long)rfirst); |
| 2967 | else |
| 2968 | Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n", |
| 2969 | (long)tfirst, (long)rfirst); |
| 2970 | |
| 2971 | if (rfirst + diff > max) |
| 2972 | max = rfirst + diff; |
| 2973 | if (!grows) |
| 2974 | grows = (tfirst < rfirst && |
| 2975 | UNISKIP(tfirst) < UNISKIP(rfirst + diff)); |
| 2976 | rfirst += diff + 1; |
| 2977 | } |
| 2978 | tfirst += diff + 1; |
| 2979 | } |
| 2980 | |
| 2981 | none = ++max; |
| 2982 | if (del) |
| 2983 | del = ++max; |
| 2984 | |
| 2985 | if (max > 0xffff) |
| 2986 | bits = 32; |
| 2987 | else if (max > 0xff) |
| 2988 | bits = 16; |
| 2989 | else |
| 2990 | bits = 8; |
| 2991 | |
| 2992 | Safefree(cPVOPo->op_pv); |
| 2993 | cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none); |
| 2994 | SvREFCNT_dec(listsv); |
| 2995 | if (transv) |
| 2996 | SvREFCNT_dec(transv); |
| 2997 | |
| 2998 | if (!del && havefinal && rlen) |
| 2999 | (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, |
| 3000 | newSVuv((UV)final), 0); |
| 3001 | |
| 3002 | if (grows) |
| 3003 | o->op_private |= OPpTRANS_GROWS; |
| 3004 | |
| 3005 | if (tsave) |
| 3006 | Safefree(tsave); |
| 3007 | if (rsave) |
| 3008 | Safefree(rsave); |
| 3009 | |
| 3010 | op_free(expr); |
| 3011 | op_free(repl); |
| 3012 | return o; |
| 3013 | } |
| 3014 | |
| 3015 | tbl = (short*)cPVOPo->op_pv; |
| 3016 | if (complement) { |
| 3017 | Zero(tbl, 256, short); |
| 3018 | for (i = 0; i < (I32)tlen; i++) |
| 3019 | tbl[t[i]] = -1; |
| 3020 | for (i = 0, j = 0; i < 256; i++) { |
| 3021 | if (!tbl[i]) { |
| 3022 | if (j >= (I32)rlen) { |
| 3023 | if (del) |
| 3024 | tbl[i] = -2; |
| 3025 | else if (rlen) |
| 3026 | tbl[i] = r[j-1]; |
| 3027 | else |
| 3028 | tbl[i] = (short)i; |
| 3029 | } |
| 3030 | else { |
| 3031 | if (i < 128 && r[j] >= 128) |
| 3032 | grows = 1; |
| 3033 | tbl[i] = r[j++]; |
| 3034 | } |
| 3035 | } |
| 3036 | } |
| 3037 | if (!del) { |
| 3038 | if (!rlen) { |
| 3039 | j = rlen; |
| 3040 | if (!squash) |
| 3041 | o->op_private |= OPpTRANS_IDENTICAL; |
| 3042 | } |
| 3043 | else if (j >= (I32)rlen) |
| 3044 | j = rlen - 1; |
| 3045 | else |
| 3046 | cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short); |
| 3047 | tbl[0x100] = rlen - j; |
| 3048 | for (i=0; i < (I32)rlen - j; i++) |
| 3049 | tbl[0x101+i] = r[j+i]; |
| 3050 | } |
| 3051 | } |
| 3052 | else { |
| 3053 | if (!rlen && !del) { |
| 3054 | r = t; rlen = tlen; |
| 3055 | if (!squash) |
| 3056 | o->op_private |= OPpTRANS_IDENTICAL; |
| 3057 | } |
| 3058 | else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) { |
| 3059 | o->op_private |= OPpTRANS_IDENTICAL; |
| 3060 | } |
| 3061 | for (i = 0; i < 256; i++) |
| 3062 | tbl[i] = -1; |
| 3063 | for (i = 0, j = 0; i < (I32)tlen; i++,j++) { |
| 3064 | if (j >= (I32)rlen) { |
| 3065 | if (del) { |
| 3066 | if (tbl[t[i]] == -1) |
| 3067 | tbl[t[i]] = -2; |
| 3068 | continue; |
| 3069 | } |
| 3070 | --j; |
| 3071 | } |
| 3072 | if (tbl[t[i]] == -1) { |
| 3073 | if (t[i] < 128 && r[j] >= 128) |
| 3074 | grows = 1; |
| 3075 | tbl[t[i]] = r[j]; |
| 3076 | } |
| 3077 | } |
| 3078 | } |
| 3079 | if (grows) |
| 3080 | o->op_private |= OPpTRANS_GROWS; |
| 3081 | op_free(expr); |
| 3082 | op_free(repl); |
| 3083 | |
| 3084 | return o; |
| 3085 | } |
| 3086 | |
| 3087 | OP * |
| 3088 | Perl_newPMOP(pTHX_ I32 type, I32 flags) |
| 3089 | { |
| 3090 | PMOP *pmop; |
| 3091 | |
| 3092 | NewOp(1101, pmop, 1, PMOP); |
| 3093 | pmop->op_type = (OPCODE)type; |
| 3094 | pmop->op_ppaddr = PL_ppaddr[type]; |
| 3095 | pmop->op_flags = (U8)flags; |
| 3096 | pmop->op_private = (U8)(0 | (flags >> 8)); |
| 3097 | |
| 3098 | if (PL_hints & HINT_RE_TAINT) |
| 3099 | pmop->op_pmpermflags |= PMf_RETAINT; |
| 3100 | if (PL_hints & HINT_LOCALE) |
| 3101 | pmop->op_pmpermflags |= PMf_LOCALE; |
| 3102 | pmop->op_pmflags = pmop->op_pmpermflags; |
| 3103 | |
| 3104 | #ifdef USE_ITHREADS |
| 3105 | { |
| 3106 | SV* repointer; |
| 3107 | if(av_len((AV*) PL_regex_pad[0]) > -1) { |
| 3108 | repointer = av_pop((AV*)PL_regex_pad[0]); |
| 3109 | pmop->op_pmoffset = SvIV(repointer); |
| 3110 | SvREPADTMP_off(repointer); |
| 3111 | sv_setiv(repointer,0); |
| 3112 | } else { |
| 3113 | repointer = newSViv(0); |
| 3114 | av_push(PL_regex_padav,SvREFCNT_inc(repointer)); |
| 3115 | pmop->op_pmoffset = av_len(PL_regex_padav); |
| 3116 | PL_regex_pad = AvARRAY(PL_regex_padav); |
| 3117 | } |
| 3118 | } |
| 3119 | #endif |
| 3120 | |
| 3121 | /* link into pm list */ |
| 3122 | if (type != OP_TRANS && PL_curstash) { |
| 3123 | pmop->op_pmnext = HvPMROOT(PL_curstash); |
| 3124 | HvPMROOT(PL_curstash) = pmop; |
| 3125 | PmopSTASH_set(pmop,PL_curstash); |
| 3126 | } |
| 3127 | |
| 3128 | return (OP*)pmop; |
| 3129 | } |
| 3130 | |
| 3131 | OP * |
| 3132 | Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl) |
| 3133 | { |
| 3134 | PMOP *pm; |
| 3135 | LOGOP *rcop; |
| 3136 | I32 repl_has_vars = 0; |
| 3137 | |
| 3138 | if (o->op_type == OP_TRANS) |
| 3139 | return pmtrans(o, expr, repl); |
| 3140 | |
| 3141 | PL_hints |= HINT_BLOCK_SCOPE; |
| 3142 | pm = (PMOP*)o; |
| 3143 | |
| 3144 | if (expr->op_type == OP_CONST) { |
| 3145 | STRLEN plen; |
| 3146 | SV *pat = ((SVOP*)expr)->op_sv; |
| 3147 | char *p = SvPV(pat, plen); |
| 3148 | if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) { |
| 3149 | sv_setpvn(pat, "\\s+", 3); |
| 3150 | p = SvPV(pat, plen); |
| 3151 | pm->op_pmflags |= PMf_SKIPWHITE; |
| 3152 | } |
| 3153 | if (DO_UTF8(pat)) |
| 3154 | pm->op_pmdynflags |= PMdf_UTF8; |
| 3155 | PM_SETRE(pm, CALLREGCOMP(aTHX_ p, p + plen, pm)); |
| 3156 | if (strEQ("\\s+", PM_GETRE(pm)->precomp)) |
| 3157 | pm->op_pmflags |= PMf_WHITE; |
| 3158 | op_free(expr); |
| 3159 | } |
| 3160 | else { |
| 3161 | if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) |
| 3162 | expr = newUNOP((!(PL_hints & HINT_RE_EVAL) |
| 3163 | ? OP_REGCRESET |
| 3164 | : OP_REGCMAYBE),0,expr); |
| 3165 | |
| 3166 | NewOp(1101, rcop, 1, LOGOP); |
| 3167 | rcop->op_type = OP_REGCOMP; |
| 3168 | rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP]; |
| 3169 | rcop->op_first = scalar(expr); |
| 3170 | rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) |
| 3171 | ? (OPf_SPECIAL | OPf_KIDS) |
| 3172 | : OPf_KIDS); |
| 3173 | rcop->op_private = 1; |
| 3174 | rcop->op_other = o; |
| 3175 | |
| 3176 | /* establish postfix order */ |
| 3177 | if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) { |
| 3178 | LINKLIST(expr); |
| 3179 | rcop->op_next = expr; |
| 3180 | ((UNOP*)expr)->op_first->op_next = (OP*)rcop; |
| 3181 | } |
| 3182 | else { |
| 3183 | rcop->op_next = LINKLIST(expr); |
| 3184 | expr->op_next = (OP*)rcop; |
| 3185 | } |
| 3186 | |
| 3187 | prepend_elem(o->op_type, scalar((OP*)rcop), o); |
| 3188 | } |
| 3189 | |
| 3190 | if (repl) { |
| 3191 | OP *curop; |
| 3192 | if (pm->op_pmflags & PMf_EVAL) { |
| 3193 | curop = 0; |
| 3194 | if (CopLINE(PL_curcop) < PL_multi_end) |
| 3195 | CopLINE_set(PL_curcop, (line_t)PL_multi_end); |
| 3196 | } |
| 3197 | #ifdef USE_5005THREADS |
| 3198 | else if (repl->op_type == OP_THREADSV |
| 3199 | && strchr("&`'123456789+", |
| 3200 | PL_threadsv_names[repl->op_targ])) |
| 3201 | { |
| 3202 | curop = 0; |
| 3203 | } |
| 3204 | #endif /* USE_5005THREADS */ |
| 3205 | else if (repl->op_type == OP_CONST) |
| 3206 | curop = repl; |
| 3207 | else { |
| 3208 | OP *lastop = 0; |
| 3209 | for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) { |
| 3210 | if (PL_opargs[curop->op_type] & OA_DANGEROUS) { |
| 3211 | #ifdef USE_5005THREADS |
| 3212 | if (curop->op_type == OP_THREADSV) { |
| 3213 | repl_has_vars = 1; |
| 3214 | if (strchr("&`'123456789+", curop->op_private)) |
| 3215 | break; |
| 3216 | } |
| 3217 | #else |
| 3218 | if (curop->op_type == OP_GV) { |
| 3219 | GV *gv = cGVOPx_gv(curop); |
| 3220 | repl_has_vars = 1; |
| 3221 | if (strchr("&`'123456789+", *GvENAME(gv))) |
| 3222 | break; |
| 3223 | } |
| 3224 | #endif /* USE_5005THREADS */ |
| 3225 | else if (curop->op_type == OP_RV2CV) |
| 3226 | break; |
| 3227 | else if (curop->op_type == OP_RV2SV || |
| 3228 | curop->op_type == OP_RV2AV || |
| 3229 | curop->op_type == OP_RV2HV || |
| 3230 | curop->op_type == OP_RV2GV) { |
| 3231 | if (lastop && lastop->op_type != OP_GV) /*funny deref?*/ |
| 3232 | break; |
| 3233 | } |
| 3234 | else if (curop->op_type == OP_PADSV || |
| 3235 | curop->op_type == OP_PADAV || |
| 3236 | curop->op_type == OP_PADHV || |
| 3237 | curop->op_type == OP_PADANY) { |
| 3238 | repl_has_vars = 1; |
| 3239 | } |
| 3240 | else if (curop->op_type == OP_PUSHRE) |
| 3241 | ; /* Okay here, dangerous in newASSIGNOP */ |
| 3242 | else |
| 3243 | break; |
| 3244 | } |
| 3245 | lastop = curop; |
| 3246 | } |
| 3247 | } |
| 3248 | if (curop == repl |
| 3249 | && !(repl_has_vars |
| 3250 | && (!PM_GETRE(pm) |
| 3251 | || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) { |
| 3252 | pm->op_pmflags |= PMf_CONST; /* const for long enough */ |
| 3253 | pm->op_pmpermflags |= PMf_CONST; /* const for long enough */ |
| 3254 | prepend_elem(o->op_type, scalar(repl), o); |
| 3255 | } |
| 3256 | else { |
| 3257 | if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */ |
| 3258 | pm->op_pmflags |= PMf_MAYBE_CONST; |
| 3259 | pm->op_pmpermflags |= PMf_MAYBE_CONST; |
| 3260 | } |
| 3261 | NewOp(1101, rcop, 1, LOGOP); |
| 3262 | rcop->op_type = OP_SUBSTCONT; |
| 3263 | rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT]; |
| 3264 | rcop->op_first = scalar(repl); |
| 3265 | rcop->op_flags |= OPf_KIDS; |
| 3266 | rcop->op_private = 1; |
| 3267 | rcop->op_other = o; |
| 3268 | |
| 3269 | /* establish postfix order */ |
| 3270 | rcop->op_next = LINKLIST(repl); |
| 3271 | repl->op_next = (OP*)rcop; |
| 3272 | |
| 3273 | pm->op_pmreplroot = scalar((OP*)rcop); |
| 3274 | pm->op_pmreplstart = LINKLIST(rcop); |
| 3275 | rcop->op_next = 0; |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | return (OP*)pm; |
| 3280 | } |
| 3281 | |
| 3282 | OP * |
| 3283 | Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv) |
| 3284 | { |
| 3285 | SVOP *svop; |
| 3286 | NewOp(1101, svop, 1, SVOP); |
| 3287 | svop->op_type = (OPCODE)type; |
| 3288 | svop->op_ppaddr = PL_ppaddr[type]; |
| 3289 | svop->op_sv = sv; |
| 3290 | svop->op_next = (OP*)svop; |
| 3291 | svop->op_flags = (U8)flags; |
| 3292 | if (PL_opargs[type] & OA_RETSCALAR) |
| 3293 | scalar((OP*)svop); |
| 3294 | if (PL_opargs[type] & OA_TARGET) |
| 3295 | svop->op_targ = pad_alloc(type, SVs_PADTMP); |
| 3296 | return CHECKOP(type, svop); |
| 3297 | } |
| 3298 | |
| 3299 | OP * |
| 3300 | Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv) |
| 3301 | { |
| 3302 | PADOP *padop; |
| 3303 | NewOp(1101, padop, 1, PADOP); |
| 3304 | padop->op_type = (OPCODE)type; |
| 3305 | padop->op_ppaddr = PL_ppaddr[type]; |
| 3306 | padop->op_padix = pad_alloc(type, SVs_PADTMP); |
| 3307 | SvREFCNT_dec(PL_curpad[padop->op_padix]); |
| 3308 | PL_curpad[padop->op_padix] = sv; |
| 3309 | SvPADTMP_on(sv); |
| 3310 | padop->op_next = (OP*)padop; |
| 3311 | padop->op_flags = (U8)flags; |
| 3312 | if (PL_opargs[type] & OA_RETSCALAR) |
| 3313 | scalar((OP*)padop); |
| 3314 | if (PL_opargs[type] & OA_TARGET) |
| 3315 | padop->op_targ = pad_alloc(type, SVs_PADTMP); |
| 3316 | return CHECKOP(type, padop); |
| 3317 | } |
| 3318 | |
| 3319 | OP * |
| 3320 | Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv) |
| 3321 | { |
| 3322 | #ifdef USE_ITHREADS |
| 3323 | GvIN_PAD_on(gv); |
| 3324 | return newPADOP(type, flags, SvREFCNT_inc(gv)); |
| 3325 | #else |
| 3326 | return newSVOP(type, flags, SvREFCNT_inc(gv)); |
| 3327 | #endif |
| 3328 | } |
| 3329 | |
| 3330 | OP * |
| 3331 | Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv) |
| 3332 | { |
| 3333 | PVOP *pvop; |
| 3334 | NewOp(1101, pvop, 1, PVOP); |
| 3335 | pvop->op_type = (OPCODE)type; |
| 3336 | pvop->op_ppaddr = PL_ppaddr[type]; |
| 3337 | pvop->op_pv = pv; |
| 3338 | pvop->op_next = (OP*)pvop; |
| 3339 | pvop->op_flags = (U8)flags; |
| 3340 | if (PL_opargs[type] & OA_RETSCALAR) |
| 3341 | scalar((OP*)pvop); |
| 3342 | if (PL_opargs[type] & OA_TARGET) |
| 3343 | pvop->op_targ = pad_alloc(type, SVs_PADTMP); |
| 3344 | return CHECKOP(type, pvop); |
| 3345 | } |
| 3346 | |
| 3347 | void |
| 3348 | Perl_package(pTHX_ OP *o) |
| 3349 | { |
| 3350 | SV *sv; |
| 3351 | |
| 3352 | save_hptr(&PL_curstash); |
| 3353 | save_item(PL_curstname); |
| 3354 | if (o) { |
| 3355 | STRLEN len; |
| 3356 | char *name; |
| 3357 | sv = cSVOPo->op_sv; |
| 3358 | name = SvPV(sv, len); |
| 3359 | PL_curstash = gv_stashpvn(name,len,TRUE); |
| 3360 | sv_setpvn(PL_curstname, name, len); |
| 3361 | op_free(o); |
| 3362 | } |
| 3363 | else { |
| 3364 | deprecate("\"package\" with no arguments"); |
| 3365 | sv_setpv(PL_curstname,"<none>"); |
| 3366 | PL_curstash = Nullhv; |
| 3367 | } |
| 3368 | PL_hints |= HINT_BLOCK_SCOPE; |
| 3369 | PL_copline = NOLINE; |
| 3370 | PL_expect = XSTATE; |
| 3371 | } |
| 3372 | |
| 3373 | void |
| 3374 | Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg) |
| 3375 | { |
| 3376 | OP *pack; |
| 3377 | OP *imop; |
| 3378 | OP *veop; |
| 3379 | |
| 3380 | if (id->op_type != OP_CONST) |
| 3381 | Perl_croak(aTHX_ "Module name must be constant"); |
| 3382 | |
| 3383 | veop = Nullop; |
| 3384 | |
| 3385 | if (version != Nullop) { |
| 3386 | SV *vesv = ((SVOP*)version)->op_sv; |
| 3387 | |
| 3388 | if (arg == Nullop && !SvNIOKp(vesv)) { |
| 3389 | arg = version; |
| 3390 | } |
| 3391 | else { |
| 3392 | OP *pack; |
| 3393 | SV *meth; |
| 3394 | |
| 3395 | if (version->op_type != OP_CONST || !SvNIOKp(vesv)) |
| 3396 | Perl_croak(aTHX_ "Version number must be constant number"); |
| 3397 | |
| 3398 | /* Make copy of id so we don't free it twice */ |
| 3399 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); |
| 3400 | |
| 3401 | /* Fake up a method call to VERSION */ |
| 3402 | meth = newSVpvn("VERSION",7); |
| 3403 | sv_upgrade(meth, SVt_PVIV); |
| 3404 | (void)SvIOK_on(meth); |
| 3405 | PERL_HASH(SvUVX(meth), (U8*)SvPVX(meth), SvCUR(meth)); |
| 3406 | veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, |
| 3407 | append_elem(OP_LIST, |
| 3408 | prepend_elem(OP_LIST, pack, list(version)), |
| 3409 | newSVOP(OP_METHOD_NAMED, 0, meth))); |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | /* Fake up an import/unimport */ |
| 3414 | if (arg && arg->op_type == OP_STUB) |
| 3415 | imop = arg; /* no import on explicit () */ |
| 3416 | else if (SvNIOKp(((SVOP*)id)->op_sv)) { |
| 3417 | imop = Nullop; /* use 5.0; */ |
| 3418 | } |
| 3419 | else { |
| 3420 | SV *meth; |
| 3421 | |
| 3422 | /* Make copy of id so we don't free it twice */ |
| 3423 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); |
| 3424 | |
| 3425 | /* Fake up a method call to import/unimport */ |
| 3426 | meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);; |
| 3427 | (void)SvUPGRADE(meth, SVt_PVIV); |
| 3428 | (void)SvIOK_on(meth); |
| 3429 | PERL_HASH(SvUVX(meth), (U8*)SvPVX(meth), SvCUR(meth)); |
| 3430 | imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, |
| 3431 | append_elem(OP_LIST, |
| 3432 | prepend_elem(OP_LIST, pack, list(arg)), |
| 3433 | newSVOP(OP_METHOD_NAMED, 0, meth))); |
| 3434 | } |
| 3435 | |
| 3436 | /* Fake up the BEGIN {}, which does its thing immediately. */ |
| 3437 | newATTRSUB(floor, |
| 3438 | newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)), |
| 3439 | Nullop, |
| 3440 | Nullop, |
| 3441 | append_elem(OP_LINESEQ, |
| 3442 | append_elem(OP_LINESEQ, |
| 3443 | newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, id)), |
| 3444 | newSTATEOP(0, Nullch, veop)), |
| 3445 | newSTATEOP(0, Nullch, imop) )); |
| 3446 | |
| 3447 | /* The "did you use incorrect case?" warning used to be here. |
| 3448 | * The problem is that on case-insensitive filesystems one |
| 3449 | * might get false positives for "use" (and "require"): |
| 3450 | * "use Strict" or "require CARP" will work. This causes |
| 3451 | * portability problems for the script: in case-strict |
| 3452 | * filesystems the script will stop working. |
| 3453 | * |
| 3454 | * The "incorrect case" warning checked whether "use Foo" |
| 3455 | * imported "Foo" to your namespace, but that is wrong, too: |
| 3456 | * there is no requirement nor promise in the language that |
| 3457 | * a Foo.pm should or would contain anything in package "Foo". |
| 3458 | * |
| 3459 | * There is very little Configure-wise that can be done, either: |
| 3460 | * the case-sensitivity of the build filesystem of Perl does not |
| 3461 | * help in guessing the case-sensitivity of the runtime environment. |
| 3462 | */ |
| 3463 | |
| 3464 | PL_hints |= HINT_BLOCK_SCOPE; |
| 3465 | PL_copline = NOLINE; |
| 3466 | PL_expect = XSTATE; |
| 3467 | } |
| 3468 | |
| 3469 | /* |
| 3470 | =head1 Embedding Functions |
| 3471 | |
| 3472 | =for apidoc load_module |
| 3473 | |
| 3474 | Loads the module whose name is pointed to by the string part of name. |
| 3475 | Note that the actual module name, not its filename, should be given. |
| 3476 | Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of |
| 3477 | PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS |
| 3478 | (or 0 for no flags). ver, if specified, provides version semantics |
| 3479 | similar to C<use Foo::Bar VERSION>. The optional trailing SV* |
| 3480 | arguments can be used to specify arguments to the module's import() |
| 3481 | method, similar to C<use Foo::Bar VERSION LIST>. |
| 3482 | |
| 3483 | =cut */ |
| 3484 | |
| 3485 | void |
| 3486 | Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...) |
| 3487 | { |
| 3488 | va_list args; |
| 3489 | va_start(args, ver); |
| 3490 | vload_module(flags, name, ver, &args); |
| 3491 | va_end(args); |
| 3492 | } |
| 3493 | |
| 3494 | #ifdef PERL_IMPLICIT_CONTEXT |
| 3495 | void |
| 3496 | Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...) |
| 3497 | { |
| 3498 | dTHX; |
| 3499 | va_list args; |
| 3500 | va_start(args, ver); |
| 3501 | vload_module(flags, name, ver, &args); |
| 3502 | va_end(args); |
| 3503 | } |
| 3504 | #endif |
| 3505 | |
| 3506 | void |
| 3507 | Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args) |
| 3508 | { |
| 3509 | OP *modname, *veop, *imop; |
| 3510 | |
| 3511 | modname = newSVOP(OP_CONST, 0, name); |
| 3512 | modname->op_private |= OPpCONST_BARE; |
| 3513 | if (ver) { |
| 3514 | veop = newSVOP(OP_CONST, 0, ver); |
| 3515 | } |
| 3516 | else |
| 3517 | veop = Nullop; |
| 3518 | if (flags & PERL_LOADMOD_NOIMPORT) { |
| 3519 | imop = sawparens(newNULLLIST()); |
| 3520 | } |
| 3521 | else if (flags & PERL_LOADMOD_IMPORT_OPS) { |
| 3522 | imop = va_arg(*args, OP*); |
| 3523 | } |
| 3524 | else { |
| 3525 | SV *sv; |
| 3526 | imop = Nullop; |
| 3527 | sv = va_arg(*args, SV*); |
| 3528 | while (sv) { |
| 3529 | imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv)); |
| 3530 | sv = va_arg(*args, SV*); |
| 3531 | } |
| 3532 | } |
| 3533 | { |
| 3534 | line_t ocopline = PL_copline; |
| 3535 | int oexpect = PL_expect; |
| 3536 | |
| 3537 | utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0), |
| 3538 | veop, modname, imop); |
| 3539 | PL_expect = oexpect; |
| 3540 | PL_copline = ocopline; |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | OP * |
| 3545 | Perl_dofile(pTHX_ OP *term) |
| 3546 | { |
| 3547 | OP *doop; |
| 3548 | GV *gv; |
| 3549 | |
| 3550 | gv = gv_fetchpv("do", FALSE, SVt_PVCV); |
| 3551 | if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) |
| 3552 | gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV); |
| 3553 | |
| 3554 | if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) { |
| 3555 | doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 3556 | append_elem(OP_LIST, term, |
| 3557 | scalar(newUNOP(OP_RV2CV, 0, |
| 3558 | newGVOP(OP_GV, 0, |
| 3559 | gv)))))); |
| 3560 | } |
| 3561 | else { |
| 3562 | doop = newUNOP(OP_DOFILE, 0, scalar(term)); |
| 3563 | } |
| 3564 | return doop; |
| 3565 | } |
| 3566 | |
| 3567 | OP * |
| 3568 | Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval) |
| 3569 | { |
| 3570 | return newBINOP(OP_LSLICE, flags, |
| 3571 | list(force_list(subscript)), |
| 3572 | list(force_list(listval)) ); |
| 3573 | } |
| 3574 | |
| 3575 | STATIC I32 |
| 3576 | S_list_assignment(pTHX_ register OP *o) |
| 3577 | { |
| 3578 | if (!o) |
| 3579 | return TRUE; |
| 3580 | |
| 3581 | if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS) |
| 3582 | o = cUNOPo->op_first; |
| 3583 | |
| 3584 | if (o->op_type == OP_COND_EXPR) { |
| 3585 | I32 t = list_assignment(cLOGOPo->op_first->op_sibling); |
| 3586 | I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling); |
| 3587 | |
| 3588 | if (t && f) |
| 3589 | return TRUE; |
| 3590 | if (t || f) |
| 3591 | yyerror("Assignment to both a list and a scalar"); |
| 3592 | return FALSE; |
| 3593 | } |
| 3594 | |
| 3595 | if (o->op_type == OP_LIST && |
| 3596 | (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR && |
| 3597 | o->op_private & OPpLVAL_INTRO) |
| 3598 | return FALSE; |
| 3599 | |
| 3600 | if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS || |
| 3601 | o->op_type == OP_RV2AV || o->op_type == OP_RV2HV || |
| 3602 | o->op_type == OP_ASLICE || o->op_type == OP_HSLICE) |
| 3603 | return TRUE; |
| 3604 | |
| 3605 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) |
| 3606 | return TRUE; |
| 3607 | |
| 3608 | if (o->op_type == OP_RV2SV) |
| 3609 | return FALSE; |
| 3610 | |
| 3611 | return FALSE; |
| 3612 | } |
| 3613 | |
| 3614 | OP * |
| 3615 | Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) |
| 3616 | { |
| 3617 | OP *o; |
| 3618 | |
| 3619 | if (optype) { |
| 3620 | if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) { |
| 3621 | return newLOGOP(optype, 0, |
| 3622 | mod(scalar(left), optype), |
| 3623 | newUNOP(OP_SASSIGN, 0, scalar(right))); |
| 3624 | } |
| 3625 | else { |
| 3626 | return newBINOP(optype, OPf_STACKED, |
| 3627 | mod(scalar(left), optype), scalar(right)); |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | if (list_assignment(left)) { |
| 3632 | OP *curop; |
| 3633 | |
| 3634 | PL_modcount = 0; |
| 3635 | PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
| 3636 | left = mod(left, OP_AASSIGN); |
| 3637 | if (PL_eval_start) |
| 3638 | PL_eval_start = 0; |
| 3639 | else { |
| 3640 | op_free(left); |
| 3641 | op_free(right); |
| 3642 | return Nullop; |
| 3643 | } |
| 3644 | curop = list(force_list(left)); |
| 3645 | o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop); |
| 3646 | o->op_private = (U8)(0 | (flags >> 8)); |
| 3647 | for (curop = ((LISTOP*)curop)->op_first; |
| 3648 | curop; curop = curop->op_sibling) |
| 3649 | { |
| 3650 | if (curop->op_type == OP_RV2HV && |
| 3651 | ((UNOP*)curop)->op_first->op_type != OP_GV) { |
| 3652 | o->op_private |= OPpASSIGN_HASH; |
| 3653 | break; |
| 3654 | } |
| 3655 | } |
| 3656 | if (!(left->op_private & OPpLVAL_INTRO)) { |
| 3657 | OP *lastop = o; |
| 3658 | PL_generation++; |
| 3659 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
| 3660 | if (PL_opargs[curop->op_type] & OA_DANGEROUS) { |
| 3661 | if (curop->op_type == OP_GV) { |
| 3662 | GV *gv = cGVOPx_gv(curop); |
| 3663 | if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation) |
| 3664 | break; |
| 3665 | SvCUR(gv) = PL_generation; |
| 3666 | } |
| 3667 | else if (curop->op_type == OP_PADSV || |
| 3668 | curop->op_type == OP_PADAV || |
| 3669 | curop->op_type == OP_PADHV || |
| 3670 | curop->op_type == OP_PADANY) { |
| 3671 | SV **svp = AvARRAY(PL_comppad_name); |
| 3672 | SV *sv = svp[curop->op_targ]; |
| 3673 | if ((int)SvCUR(sv) == PL_generation) |
| 3674 | break; |
| 3675 | SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */ |
| 3676 | } |
| 3677 | else if (curop->op_type == OP_RV2CV) |
| 3678 | break; |
| 3679 | else if (curop->op_type == OP_RV2SV || |
| 3680 | curop->op_type == OP_RV2AV || |
| 3681 | curop->op_type == OP_RV2HV || |
| 3682 | curop->op_type == OP_RV2GV) { |
| 3683 | if (lastop->op_type != OP_GV) /* funny deref? */ |
| 3684 | break; |
| 3685 | } |
| 3686 | else if (curop->op_type == OP_PUSHRE) { |
| 3687 | if (((PMOP*)curop)->op_pmreplroot) { |
| 3688 | #ifdef USE_ITHREADS |
| 3689 | GV *gv = (GV*)PL_curpad[INT2PTR(PADOFFSET,((PMOP*)curop)->op_pmreplroot)]; |
| 3690 | #else |
| 3691 | GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot; |
| 3692 | #endif |
| 3693 | if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation) |
| 3694 | break; |
| 3695 | SvCUR(gv) = PL_generation; |
| 3696 | } |
| 3697 | } |
| 3698 | else |
| 3699 | break; |
| 3700 | } |
| 3701 | lastop = curop; |
| 3702 | } |
| 3703 | if (curop != o) |
| 3704 | o->op_private |= OPpASSIGN_COMMON; |
| 3705 | } |
| 3706 | if (right && right->op_type == OP_SPLIT) { |
| 3707 | OP* tmpop; |
| 3708 | if ((tmpop = ((LISTOP*)right)->op_first) && |
| 3709 | tmpop->op_type == OP_PUSHRE) |
| 3710 | { |
| 3711 | PMOP *pm = (PMOP*)tmpop; |
| 3712 | if (left->op_type == OP_RV2AV && |
| 3713 | !(left->op_private & OPpLVAL_INTRO) && |
| 3714 | !(o->op_private & OPpASSIGN_COMMON) ) |
| 3715 | { |
| 3716 | tmpop = ((UNOP*)left)->op_first; |
| 3717 | if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) { |
| 3718 | #ifdef USE_ITHREADS |
| 3719 | pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix); |
| 3720 | cPADOPx(tmpop)->op_padix = 0; /* steal it */ |
| 3721 | #else |
| 3722 | pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv; |
| 3723 | cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */ |
| 3724 | #endif |
| 3725 | pm->op_pmflags |= PMf_ONCE; |
| 3726 | tmpop = cUNOPo->op_first; /* to list (nulled) */ |
| 3727 | tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ |
| 3728 | tmpop->op_sibling = Nullop; /* don't free split */ |
| 3729 | right->op_next = tmpop->op_next; /* fix starting loc */ |
| 3730 | op_free(o); /* blow off assign */ |
| 3731 | right->op_flags &= ~OPf_WANT; |
| 3732 | /* "I don't know and I don't care." */ |
| 3733 | return right; |
| 3734 | } |
| 3735 | } |
| 3736 | else { |
| 3737 | if (PL_modcount < RETURN_UNLIMITED_NUMBER && |
| 3738 | ((LISTOP*)right)->op_last->op_type == OP_CONST) |
| 3739 | { |
| 3740 | SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv; |
| 3741 | if (SvIVX(sv) == 0) |
| 3742 | sv_setiv(sv, PL_modcount+1); |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | } |
| 3747 | return o; |
| 3748 | } |
| 3749 | if (!right) |
| 3750 | right = newOP(OP_UNDEF, 0); |
| 3751 | if (right->op_type == OP_READLINE) { |
| 3752 | right->op_flags |= OPf_STACKED; |
| 3753 | return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right)); |
| 3754 | } |
| 3755 | else { |
| 3756 | PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
| 3757 | o = newBINOP(OP_SASSIGN, flags, |
| 3758 | scalar(right), mod(scalar(left), OP_SASSIGN) ); |
| 3759 | if (PL_eval_start) |
| 3760 | PL_eval_start = 0; |
| 3761 | else { |
| 3762 | op_free(o); |
| 3763 | return Nullop; |
| 3764 | } |
| 3765 | } |
| 3766 | return o; |
| 3767 | } |
| 3768 | |
| 3769 | OP * |
| 3770 | Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o) |
| 3771 | { |
| 3772 | U32 seq = intro_my(); |
| 3773 | register COP *cop; |
| 3774 | |
| 3775 | NewOp(1101, cop, 1, COP); |
| 3776 | if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) { |
| 3777 | cop->op_type = OP_DBSTATE; |
| 3778 | cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ]; |
| 3779 | } |
| 3780 | else { |
| 3781 | cop->op_type = OP_NEXTSTATE; |
| 3782 | cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ]; |
| 3783 | } |
| 3784 | cop->op_flags = (U8)flags; |
| 3785 | cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK); |
| 3786 | #ifdef NATIVE_HINTS |
| 3787 | cop->op_private |= NATIVE_HINTS; |
| 3788 | #endif |
| 3789 | PL_compiling.op_private = cop->op_private; |
| 3790 | cop->op_next = (OP*)cop; |
| 3791 | |
| 3792 | if (label) { |
| 3793 | cop->cop_label = label; |
| 3794 | PL_hints |= HINT_BLOCK_SCOPE; |
| 3795 | } |
| 3796 | cop->cop_seq = seq; |
| 3797 | cop->cop_arybase = PL_curcop->cop_arybase; |
| 3798 | if (specialWARN(PL_curcop->cop_warnings)) |
| 3799 | cop->cop_warnings = PL_curcop->cop_warnings ; |
| 3800 | else |
| 3801 | cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ; |
| 3802 | if (specialCopIO(PL_curcop->cop_io)) |
| 3803 | cop->cop_io = PL_curcop->cop_io; |
| 3804 | else |
| 3805 | cop->cop_io = newSVsv(PL_curcop->cop_io) ; |
| 3806 | |
| 3807 | |
| 3808 | if (PL_copline == NOLINE) |
| 3809 | CopLINE_set(cop, CopLINE(PL_curcop)); |
| 3810 | else { |
| 3811 | CopLINE_set(cop, PL_copline); |
| 3812 | PL_copline = NOLINE; |
| 3813 | } |
| 3814 | #ifdef USE_ITHREADS |
| 3815 | CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */ |
| 3816 | #else |
| 3817 | CopFILEGV_set(cop, CopFILEGV(PL_curcop)); |
| 3818 | #endif |
| 3819 | CopSTASH_set(cop, PL_curstash); |
| 3820 | |
| 3821 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
| 3822 | SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE); |
| 3823 | if (svp && *svp != &PL_sv_undef ) { |
| 3824 | (void)SvIOK_on(*svp); |
| 3825 | SvIVX(*svp) = PTR2IV(cop); |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | return prepend_elem(OP_LINESEQ, (OP*)cop, o); |
| 3830 | } |
| 3831 | |
| 3832 | /* "Introduce" my variables to visible status. */ |
| 3833 | U32 |
| 3834 | Perl_intro_my(pTHX) |
| 3835 | { |
| 3836 | SV **svp; |
| 3837 | SV *sv; |
| 3838 | I32 i; |
| 3839 | |
| 3840 | if (! PL_min_intro_pending) |
| 3841 | return PL_cop_seqmax; |
| 3842 | |
| 3843 | svp = AvARRAY(PL_comppad_name); |
| 3844 | for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) { |
| 3845 | if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) { |
| 3846 | SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */ |
| 3847 | SvNVX(sv) = (NV)PL_cop_seqmax; |
| 3848 | } |
| 3849 | } |
| 3850 | PL_min_intro_pending = 0; |
| 3851 | PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */ |
| 3852 | return PL_cop_seqmax++; |
| 3853 | } |
| 3854 | |
| 3855 | OP * |
| 3856 | Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other) |
| 3857 | { |
| 3858 | return new_logop(type, flags, &first, &other); |
| 3859 | } |
| 3860 | |
| 3861 | STATIC OP * |
| 3862 | S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp) |
| 3863 | { |
| 3864 | LOGOP *logop; |
| 3865 | OP *o; |
| 3866 | OP *first = *firstp; |
| 3867 | OP *other = *otherp; |
| 3868 | |
| 3869 | if (type == OP_XOR) /* Not short circuit, but here by precedence. */ |
| 3870 | return newBINOP(type, flags, scalar(first), scalar(other)); |
| 3871 | |
| 3872 | scalarboolean(first); |
| 3873 | /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */ |
| 3874 | if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) { |
| 3875 | if (type == OP_AND || type == OP_OR) { |
| 3876 | if (type == OP_AND) |
| 3877 | type = OP_OR; |
| 3878 | else |
| 3879 | type = OP_AND; |
| 3880 | o = first; |
| 3881 | first = *firstp = cUNOPo->op_first; |
| 3882 | if (o->op_next) |
| 3883 | first->op_next = o->op_next; |
| 3884 | cUNOPo->op_first = Nullop; |
| 3885 | op_free(o); |
| 3886 | } |
| 3887 | } |
| 3888 | if (first->op_type == OP_CONST) { |
| 3889 | if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE)) |
| 3890 | Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional"); |
| 3891 | if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) { |
| 3892 | op_free(first); |
| 3893 | *firstp = Nullop; |
| 3894 | return other; |
| 3895 | } |
| 3896 | else { |
| 3897 | op_free(other); |
| 3898 | *otherp = Nullop; |
| 3899 | return first; |
| 3900 | } |
| 3901 | } |
| 3902 | else if (first->op_type == OP_WANTARRAY) { |
| 3903 | if (type == OP_AND) |
| 3904 | list(other); |
| 3905 | else |
| 3906 | scalar(other); |
| 3907 | } |
| 3908 | else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) { |
| 3909 | OP *k1 = ((UNOP*)first)->op_first; |
| 3910 | OP *k2 = k1->op_sibling; |
| 3911 | OPCODE warnop = 0; |
| 3912 | switch (first->op_type) |
| 3913 | { |
| 3914 | case OP_NULL: |
| 3915 | if (k2 && k2->op_type == OP_READLINE |
| 3916 | && (k2->op_flags & OPf_STACKED) |
| 3917 | && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) |
| 3918 | { |
| 3919 | warnop = k2->op_type; |
| 3920 | } |
| 3921 | break; |
| 3922 | |
| 3923 | case OP_SASSIGN: |
| 3924 | if (k1->op_type == OP_READDIR |
| 3925 | || k1->op_type == OP_GLOB |
| 3926 | || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) |
| 3927 | || k1->op_type == OP_EACH) |
| 3928 | { |
| 3929 | warnop = ((k1->op_type == OP_NULL) |
| 3930 | ? (OPCODE)k1->op_targ : k1->op_type); |
| 3931 | } |
| 3932 | break; |
| 3933 | } |
| 3934 | if (warnop) { |
| 3935 | line_t oldline = CopLINE(PL_curcop); |
| 3936 | CopLINE_set(PL_curcop, PL_copline); |
| 3937 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
| 3938 | "Value of %s%s can be \"0\"; test with defined()", |
| 3939 | PL_op_desc[warnop], |
| 3940 | ((warnop == OP_READLINE || warnop == OP_GLOB) |
| 3941 | ? " construct" : "() operator")); |
| 3942 | CopLINE_set(PL_curcop, oldline); |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | if (!other) |
| 3947 | return first; |
| 3948 | |
| 3949 | if (type == OP_ANDASSIGN || type == OP_ORASSIGN) |
| 3950 | other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ |
| 3951 | |
| 3952 | NewOp(1101, logop, 1, LOGOP); |
| 3953 | |
| 3954 | logop->op_type = (OPCODE)type; |
| 3955 | logop->op_ppaddr = PL_ppaddr[type]; |
| 3956 | logop->op_first = first; |
| 3957 | logop->op_flags = flags | OPf_KIDS; |
| 3958 | logop->op_other = LINKLIST(other); |
| 3959 | logop->op_private = (U8)(1 | (flags >> 8)); |
| 3960 | |
| 3961 | /* establish postfix order */ |
| 3962 | logop->op_next = LINKLIST(first); |
| 3963 | first->op_next = (OP*)logop; |
| 3964 | first->op_sibling = other; |
| 3965 | |
| 3966 | o = newUNOP(OP_NULL, 0, (OP*)logop); |
| 3967 | other->op_next = o; |
| 3968 | |
| 3969 | return o; |
| 3970 | } |
| 3971 | |
| 3972 | OP * |
| 3973 | Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop) |
| 3974 | { |
| 3975 | LOGOP *logop; |
| 3976 | OP *start; |
| 3977 | OP *o; |
| 3978 | |
| 3979 | if (!falseop) |
| 3980 | return newLOGOP(OP_AND, 0, first, trueop); |
| 3981 | if (!trueop) |
| 3982 | return newLOGOP(OP_OR, 0, first, falseop); |
| 3983 | |
| 3984 | scalarboolean(first); |
| 3985 | if (first->op_type == OP_CONST) { |
| 3986 | if (SvTRUE(((SVOP*)first)->op_sv)) { |
| 3987 | op_free(first); |
| 3988 | op_free(falseop); |
| 3989 | return trueop; |
| 3990 | } |
| 3991 | else { |
| 3992 | op_free(first); |
| 3993 | op_free(trueop); |
| 3994 | return falseop; |
| 3995 | } |
| 3996 | } |
| 3997 | else if (first->op_type == OP_WANTARRAY) { |
| 3998 | list(trueop); |
| 3999 | scalar(falseop); |
| 4000 | } |
| 4001 | NewOp(1101, logop, 1, LOGOP); |
| 4002 | logop->op_type = OP_COND_EXPR; |
| 4003 | logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR]; |
| 4004 | logop->op_first = first; |
| 4005 | logop->op_flags = flags | OPf_KIDS; |
| 4006 | logop->op_private = (U8)(1 | (flags >> 8)); |
| 4007 | logop->op_other = LINKLIST(trueop); |
| 4008 | logop->op_next = LINKLIST(falseop); |
| 4009 | |
| 4010 | |
| 4011 | /* establish postfix order */ |
| 4012 | start = LINKLIST(first); |
| 4013 | first->op_next = (OP*)logop; |
| 4014 | |
| 4015 | first->op_sibling = trueop; |
| 4016 | trueop->op_sibling = falseop; |
| 4017 | o = newUNOP(OP_NULL, 0, (OP*)logop); |
| 4018 | |
| 4019 | trueop->op_next = falseop->op_next = o; |
| 4020 | |
| 4021 | o->op_next = start; |
| 4022 | return o; |
| 4023 | } |
| 4024 | |
| 4025 | OP * |
| 4026 | Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right) |
| 4027 | { |
| 4028 | LOGOP *range; |
| 4029 | OP *flip; |
| 4030 | OP *flop; |
| 4031 | OP *leftstart; |
| 4032 | OP *o; |
| 4033 | |
| 4034 | NewOp(1101, range, 1, LOGOP); |
| 4035 | |
| 4036 | range->op_type = OP_RANGE; |
| 4037 | range->op_ppaddr = PL_ppaddr[OP_RANGE]; |
| 4038 | range->op_first = left; |
| 4039 | range->op_flags = OPf_KIDS; |
| 4040 | leftstart = LINKLIST(left); |
| 4041 | range->op_other = LINKLIST(right); |
| 4042 | range->op_private = (U8)(1 | (flags >> 8)); |
| 4043 | |
| 4044 | left->op_sibling = right; |
| 4045 | |
| 4046 | range->op_next = (OP*)range; |
| 4047 | flip = newUNOP(OP_FLIP, flags, (OP*)range); |
| 4048 | flop = newUNOP(OP_FLOP, 0, flip); |
| 4049 | o = newUNOP(OP_NULL, 0, flop); |
| 4050 | linklist(flop); |
| 4051 | range->op_next = leftstart; |
| 4052 | |
| 4053 | left->op_next = flip; |
| 4054 | right->op_next = flop; |
| 4055 | |
| 4056 | range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
| 4057 | sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV); |
| 4058 | flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
| 4059 | sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); |
| 4060 | |
| 4061 | flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; |
| 4062 | flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; |
| 4063 | |
| 4064 | flip->op_next = o; |
| 4065 | if (!flip->op_private || !flop->op_private) |
| 4066 | linklist(o); /* blow off optimizer unless constant */ |
| 4067 | |
| 4068 | return o; |
| 4069 | } |
| 4070 | |
| 4071 | OP * |
| 4072 | Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block) |
| 4073 | { |
| 4074 | OP* listop; |
| 4075 | OP* o; |
| 4076 | int once = block && block->op_flags & OPf_SPECIAL && |
| 4077 | (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL); |
| 4078 | |
| 4079 | if (expr) { |
| 4080 | if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) |
| 4081 | return block; /* do {} while 0 does once */ |
| 4082 | if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
| 4083 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { |
| 4084 | expr = newUNOP(OP_DEFINED, 0, |
| 4085 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
| 4086 | } else if (expr->op_flags & OPf_KIDS) { |
| 4087 | OP *k1 = ((UNOP*)expr)->op_first; |
| 4088 | OP *k2 = (k1) ? k1->op_sibling : NULL; |
| 4089 | switch (expr->op_type) { |
| 4090 | case OP_NULL: |
| 4091 | if (k2 && k2->op_type == OP_READLINE |
| 4092 | && (k2->op_flags & OPf_STACKED) |
| 4093 | && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) |
| 4094 | expr = newUNOP(OP_DEFINED, 0, expr); |
| 4095 | break; |
| 4096 | |
| 4097 | case OP_SASSIGN: |
| 4098 | if (k1->op_type == OP_READDIR |
| 4099 | || k1->op_type == OP_GLOB |
| 4100 | || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) |
| 4101 | || k1->op_type == OP_EACH) |
| 4102 | expr = newUNOP(OP_DEFINED, 0, expr); |
| 4103 | break; |
| 4104 | } |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); |
| 4109 | o = new_logop(OP_AND, 0, &expr, &listop); |
| 4110 | |
| 4111 | if (listop) |
| 4112 | ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); |
| 4113 | |
| 4114 | if (once && o != listop) |
| 4115 | o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; |
| 4116 | |
| 4117 | if (o == listop) |
| 4118 | o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ |
| 4119 | |
| 4120 | o->op_flags |= flags; |
| 4121 | o = scope(o); |
| 4122 | o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/ |
| 4123 | return o; |
| 4124 | } |
| 4125 | |
| 4126 | OP * |
| 4127 | Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont) |
| 4128 | { |
| 4129 | OP *redo; |
| 4130 | OP *next = 0; |
| 4131 | OP *listop; |
| 4132 | OP *o; |
| 4133 | U8 loopflags = 0; |
| 4134 | |
| 4135 | if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
| 4136 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) { |
| 4137 | expr = newUNOP(OP_DEFINED, 0, |
| 4138 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
| 4139 | } else if (expr && (expr->op_flags & OPf_KIDS)) { |
| 4140 | OP *k1 = ((UNOP*)expr)->op_first; |
| 4141 | OP *k2 = (k1) ? k1->op_sibling : NULL; |
| 4142 | switch (expr->op_type) { |
| 4143 | case OP_NULL: |
| 4144 | if (k2 && k2->op_type == OP_READLINE |
| 4145 | && (k2->op_flags & OPf_STACKED) |
| 4146 | && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) |
| 4147 | expr = newUNOP(OP_DEFINED, 0, expr); |
| 4148 | break; |
| 4149 | |
| 4150 | case OP_SASSIGN: |
| 4151 | if (k1->op_type == OP_READDIR |
| 4152 | || k1->op_type == OP_GLOB |
| 4153 | || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) |
| 4154 | || k1->op_type == OP_EACH) |
| 4155 | expr = newUNOP(OP_DEFINED, 0, expr); |
| 4156 | break; |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (!block) |
| 4161 | block = newOP(OP_NULL, 0); |
| 4162 | else if (cont) { |
| 4163 | block = scope(block); |
| 4164 | } |
| 4165 | |
| 4166 | if (cont) { |
| 4167 | next = LINKLIST(cont); |
| 4168 | } |
| 4169 | if (expr) { |
| 4170 | OP *unstack = newOP(OP_UNSTACK, 0); |
| 4171 | if (!next) |
| 4172 | next = unstack; |
| 4173 | cont = append_elem(OP_LINESEQ, cont, unstack); |
| 4174 | if ((line_t)whileline != NOLINE) { |
| 4175 | PL_copline = (line_t)whileline; |
| 4176 | cont = append_elem(OP_LINESEQ, cont, |
| 4177 | newSTATEOP(0, Nullch, Nullop)); |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont); |
| 4182 | redo = LINKLIST(listop); |
| 4183 | |
| 4184 | if (expr) { |
| 4185 | PL_copline = (line_t)whileline; |
| 4186 | scalar(listop); |
| 4187 | o = new_logop(OP_AND, 0, &expr, &listop); |
| 4188 | if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { |
| 4189 | op_free(expr); /* oops, it's a while (0) */ |
| 4190 | op_free((OP*)loop); |
| 4191 | return Nullop; /* listop already freed by new_logop */ |
| 4192 | } |
| 4193 | if (listop) |
| 4194 | ((LISTOP*)listop)->op_last->op_next = |
| 4195 | (o == listop ? redo : LINKLIST(o)); |
| 4196 | } |
| 4197 | else |
| 4198 | o = listop; |
| 4199 | |
| 4200 | if (!loop) { |
| 4201 | NewOp(1101,loop,1,LOOP); |
| 4202 | loop->op_type = OP_ENTERLOOP; |
| 4203 | loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP]; |
| 4204 | loop->op_private = 0; |
| 4205 | loop->op_next = (OP*)loop; |
| 4206 | } |
| 4207 | |
| 4208 | o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); |
| 4209 | |
| 4210 | loop->op_redoop = redo; |
| 4211 | loop->op_lastop = o; |
| 4212 | o->op_private |= loopflags; |
| 4213 | |
| 4214 | if (next) |
| 4215 | loop->op_nextop = next; |
| 4216 | else |
| 4217 | loop->op_nextop = o; |
| 4218 | |
| 4219 | o->op_flags |= flags; |
| 4220 | o->op_private |= (flags >> 8); |
| 4221 | return o; |
| 4222 | } |
| 4223 | |
| 4224 | OP * |
| 4225 | Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont) |
| 4226 | { |
| 4227 | LOOP *loop; |
| 4228 | OP *wop; |
| 4229 | PADOFFSET padoff = 0; |
| 4230 | I32 iterflags = 0; |
| 4231 | |
| 4232 | if (sv) { |
| 4233 | if (sv->op_type == OP_RV2SV) { /* symbol table variable */ |
| 4234 | sv->op_type = OP_RV2GV; |
| 4235 | sv->op_ppaddr = PL_ppaddr[OP_RV2GV]; |
| 4236 | } |
| 4237 | else if (sv->op_type == OP_PADSV) { /* private variable */ |
| 4238 | padoff = sv->op_targ; |
| 4239 | sv->op_targ = 0; |
| 4240 | op_free(sv); |
| 4241 | sv = Nullop; |
| 4242 | } |
| 4243 | else if (sv->op_type == OP_THREADSV) { /* per-thread variable */ |
| 4244 | padoff = sv->op_targ; |
| 4245 | sv->op_targ = 0; |
| 4246 | iterflags |= OPf_SPECIAL; |
| 4247 | op_free(sv); |
| 4248 | sv = Nullop; |
| 4249 | } |
| 4250 | else |
| 4251 | Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]); |
| 4252 | } |
| 4253 | else { |
| 4254 | #ifdef USE_5005THREADS |
| 4255 | padoff = find_threadsv("_"); |
| 4256 | iterflags |= OPf_SPECIAL; |
| 4257 | #else |
| 4258 | sv = newGVOP(OP_GV, 0, PL_defgv); |
| 4259 | #endif |
| 4260 | } |
| 4261 | if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { |
| 4262 | expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART); |
| 4263 | iterflags |= OPf_STACKED; |
| 4264 | } |
| 4265 | else if (expr->op_type == OP_NULL && |
| 4266 | (expr->op_flags & OPf_KIDS) && |
| 4267 | ((BINOP*)expr)->op_first->op_type == OP_FLOP) |
| 4268 | { |
| 4269 | /* Basically turn for($x..$y) into the same as for($x,$y), but we |
| 4270 | * set the STACKED flag to indicate that these values are to be |
| 4271 | * treated as min/max values by 'pp_iterinit'. |
| 4272 | */ |
| 4273 | UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first; |
| 4274 | LOGOP* range = (LOGOP*) flip->op_first; |
| 4275 | OP* left = range->op_first; |
| 4276 | OP* right = left->op_sibling; |
| 4277 | LISTOP* listop; |
| 4278 | |
| 4279 | range->op_flags &= ~OPf_KIDS; |
| 4280 | range->op_first = Nullop; |
| 4281 | |
| 4282 | listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right); |
| 4283 | listop->op_first->op_next = range->op_next; |
| 4284 | left->op_next = range->op_other; |
| 4285 | right->op_next = (OP*)listop; |
| 4286 | listop->op_next = listop->op_first; |
| 4287 | |
| 4288 | op_free(expr); |
| 4289 | expr = (OP*)(listop); |
| 4290 | op_null(expr); |
| 4291 | iterflags |= OPf_STACKED; |
| 4292 | } |
| 4293 | else { |
| 4294 | expr = mod(force_list(expr), OP_GREPSTART); |
| 4295 | } |
| 4296 | |
| 4297 | |
| 4298 | loop = (LOOP*)list(convert(OP_ENTERITER, iterflags, |
| 4299 | append_elem(OP_LIST, expr, scalar(sv)))); |
| 4300 | assert(!loop->op_next); |
| 4301 | #ifdef PL_OP_SLAB_ALLOC |
| 4302 | { |
| 4303 | LOOP *tmp; |
| 4304 | NewOp(1234,tmp,1,LOOP); |
| 4305 | Copy(loop,tmp,1,LOOP); |
| 4306 | FreeOp(loop); |
| 4307 | loop = tmp; |
| 4308 | } |
| 4309 | #else |
| 4310 | Renew(loop, 1, LOOP); |
| 4311 | #endif |
| 4312 | loop->op_targ = padoff; |
| 4313 | wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont); |
| 4314 | PL_copline = forline; |
| 4315 | return newSTATEOP(0, label, wop); |
| 4316 | } |
| 4317 | |
| 4318 | OP* |
| 4319 | Perl_newLOOPEX(pTHX_ I32 type, OP *label) |
| 4320 | { |
| 4321 | OP *o; |
| 4322 | STRLEN n_a; |
| 4323 | |
| 4324 | if (type != OP_GOTO || label->op_type == OP_CONST) { |
| 4325 | /* "last()" means "last" */ |
| 4326 | if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) |
| 4327 | o = newOP(type, OPf_SPECIAL); |
| 4328 | else { |
| 4329 | o = newPVOP(type, 0, savepv(label->op_type == OP_CONST |
| 4330 | ? SvPVx(((SVOP*)label)->op_sv, n_a) |
| 4331 | : "")); |
| 4332 | } |
| 4333 | op_free(label); |
| 4334 | } |
| 4335 | else { |
| 4336 | if (label->op_type == OP_ENTERSUB) |
| 4337 | label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN)); |
| 4338 | o = newUNOP(type, OPf_STACKED, label); |
| 4339 | } |
| 4340 | PL_hints |= HINT_BLOCK_SCOPE; |
| 4341 | return o; |
| 4342 | } |
| 4343 | |
| 4344 | void |
| 4345 | Perl_cv_undef(pTHX_ CV *cv) |
| 4346 | { |
| 4347 | #ifdef USE_5005THREADS |
| 4348 | if (CvMUTEXP(cv)) { |
| 4349 | MUTEX_DESTROY(CvMUTEXP(cv)); |
| 4350 | Safefree(CvMUTEXP(cv)); |
| 4351 | CvMUTEXP(cv) = 0; |
| 4352 | } |
| 4353 | #endif /* USE_5005THREADS */ |
| 4354 | |
| 4355 | #ifdef USE_ITHREADS |
| 4356 | if (CvFILE(cv) && !CvXSUB(cv)) { |
| 4357 | /* for XSUBs CvFILE point directly to static memory; __FILE__ */ |
| 4358 | Safefree(CvFILE(cv)); |
| 4359 | } |
| 4360 | CvFILE(cv) = 0; |
| 4361 | #endif |
| 4362 | |
| 4363 | if (!CvXSUB(cv) && CvROOT(cv)) { |
| 4364 | #ifdef USE_5005THREADS |
| 4365 | if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr)) |
| 4366 | Perl_croak(aTHX_ "Can't undef active subroutine"); |
| 4367 | #else |
| 4368 | if (CvDEPTH(cv)) |
| 4369 | Perl_croak(aTHX_ "Can't undef active subroutine"); |
| 4370 | #endif /* USE_5005THREADS */ |
| 4371 | ENTER; |
| 4372 | |
| 4373 | SAVEVPTR(PL_curpad); |
| 4374 | PL_curpad = 0; |
| 4375 | |
| 4376 | op_free(CvROOT(cv)); |
| 4377 | CvROOT(cv) = Nullop; |
| 4378 | LEAVE; |
| 4379 | } |
| 4380 | SvPOK_off((SV*)cv); /* forget prototype */ |
| 4381 | CvGV(cv) = Nullgv; |
| 4382 | /* Since closure prototypes have the same lifetime as the containing |
| 4383 | * CV, they don't hold a refcount on the outside CV. This avoids |
| 4384 | * the refcount loop between the outer CV (which keeps a refcount to |
| 4385 | * the closure prototype in the pad entry for pp_anoncode()) and the |
| 4386 | * closure prototype, and the ensuing memory leak. --GSAR */ |
| 4387 | if (!CvANON(cv) || CvCLONED(cv)) |
| 4388 | SvREFCNT_dec(CvOUTSIDE(cv)); |
| 4389 | CvOUTSIDE(cv) = Nullcv; |
| 4390 | if (CvCONST(cv)) { |
| 4391 | SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr); |
| 4392 | CvCONST_off(cv); |
| 4393 | } |
| 4394 | if (CvPADLIST(cv)) { |
| 4395 | /* may be during global destruction */ |
| 4396 | if (SvREFCNT(CvPADLIST(cv))) { |
| 4397 | I32 i = AvFILLp(CvPADLIST(cv)); |
| 4398 | while (i >= 0) { |
| 4399 | SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE); |
| 4400 | SV* sv = svp ? *svp : Nullsv; |
| 4401 | if (!sv) |
| 4402 | continue; |
| 4403 | if (sv == (SV*)PL_comppad_name) |
| 4404 | PL_comppad_name = Nullav; |
| 4405 | else if (sv == (SV*)PL_comppad) { |
| 4406 | PL_comppad = Nullav; |
| 4407 | PL_curpad = Null(SV**); |
| 4408 | } |
| 4409 | SvREFCNT_dec(sv); |
| 4410 | } |
| 4411 | SvREFCNT_dec((SV*)CvPADLIST(cv)); |
| 4412 | } |
| 4413 | CvPADLIST(cv) = Nullav; |
| 4414 | } |
| 4415 | if (CvXSUB(cv)) { |
| 4416 | CvXSUB(cv) = 0; |
| 4417 | } |
| 4418 | CvFLAGS(cv) = 0; |
| 4419 | } |
| 4420 | |
| 4421 | #ifdef DEBUG_CLOSURES |
| 4422 | STATIC void |
| 4423 | S_cv_dump(pTHX_ CV *cv) |
| 4424 | { |
| 4425 | #ifdef DEBUGGING |
| 4426 | CV *outside = CvOUTSIDE(cv); |
| 4427 | AV* padlist = CvPADLIST(cv); |
| 4428 | AV* pad_name; |
| 4429 | AV* pad; |
| 4430 | SV** pname; |
| 4431 | SV** ppad; |
| 4432 | I32 ix; |
| 4433 | |
| 4434 | PerlIO_printf(Perl_debug_log, |
| 4435 | "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n", |
| 4436 | PTR2UV(cv), |
| 4437 | (CvANON(cv) ? "ANON" |
| 4438 | : (cv == PL_main_cv) ? "MAIN" |
| 4439 | : CvUNIQUE(cv) ? "UNIQUE" |
| 4440 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), |
| 4441 | PTR2UV(outside), |
| 4442 | (!outside ? "null" |
| 4443 | : CvANON(outside) ? "ANON" |
| 4444 | : (outside == PL_main_cv) ? "MAIN" |
| 4445 | : CvUNIQUE(outside) ? "UNIQUE" |
| 4446 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); |
| 4447 | |
| 4448 | if (!padlist) |
| 4449 | return; |
| 4450 | |
| 4451 | pad_name = (AV*)*av_fetch(padlist, 0, FALSE); |
| 4452 | pad = (AV*)*av_fetch(padlist, 1, FALSE); |
| 4453 | pname = AvARRAY(pad_name); |
| 4454 | ppad = AvARRAY(pad); |
| 4455 | |
| 4456 | for (ix = 1; ix <= AvFILLp(pad_name); ix++) { |
| 4457 | if (SvPOK(pname[ix])) |
| 4458 | PerlIO_printf(Perl_debug_log, |
| 4459 | "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n", |
| 4460 | (int)ix, PTR2UV(ppad[ix]), |
| 4461 | SvFAKE(pname[ix]) ? "FAKE " : "", |
| 4462 | SvPVX(pname[ix]), |
| 4463 | (IV)I_32(SvNVX(pname[ix])), |
| 4464 | SvIVX(pname[ix])); |
| 4465 | } |
| 4466 | #endif /* DEBUGGING */ |
| 4467 | } |
| 4468 | #endif /* DEBUG_CLOSURES */ |
| 4469 | |
| 4470 | STATIC CV * |
| 4471 | S_cv_clone2(pTHX_ CV *proto, CV *outside) |
| 4472 | { |
| 4473 | AV* av; |
| 4474 | I32 ix; |
| 4475 | AV* protopadlist = CvPADLIST(proto); |
| 4476 | AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE); |
| 4477 | AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE); |
| 4478 | SV** pname = AvARRAY(protopad_name); |
| 4479 | SV** ppad = AvARRAY(protopad); |
| 4480 | I32 fname = AvFILLp(protopad_name); |
| 4481 | I32 fpad = AvFILLp(protopad); |
| 4482 | AV* comppadlist; |
| 4483 | CV* cv; |
| 4484 | |
| 4485 | assert(!CvUNIQUE(proto)); |
| 4486 | |
| 4487 | ENTER; |
| 4488 | SAVECOMPPAD(); |
| 4489 | SAVESPTR(PL_comppad_name); |
| 4490 | SAVESPTR(PL_compcv); |
| 4491 | |
| 4492 | cv = PL_compcv = (CV*)NEWSV(1104,0); |
| 4493 | sv_upgrade((SV *)cv, SvTYPE(proto)); |
| 4494 | CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE; |
| 4495 | CvCLONED_on(cv); |
| 4496 | |
| 4497 | #ifdef USE_5005THREADS |
| 4498 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
| 4499 | MUTEX_INIT(CvMUTEXP(cv)); |
| 4500 | CvOWNER(cv) = 0; |
| 4501 | #endif /* USE_5005THREADS */ |
| 4502 | #ifdef USE_ITHREADS |
| 4503 | CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto) |
| 4504 | : savepv(CvFILE(proto)); |
| 4505 | #else |
| 4506 | CvFILE(cv) = CvFILE(proto); |
| 4507 | #endif |
| 4508 | CvGV(cv) = CvGV(proto); |
| 4509 | CvSTASH(cv) = CvSTASH(proto); |
| 4510 | CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); |
| 4511 | CvSTART(cv) = CvSTART(proto); |
| 4512 | if (outside) |
| 4513 | CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside); |
| 4514 | |
| 4515 | if (SvPOK(proto)) |
| 4516 | sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto)); |
| 4517 | |
| 4518 | PL_comppad_name = newAV(); |
| 4519 | for (ix = fname; ix >= 0; ix--) |
| 4520 | av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix])); |
| 4521 | |
| 4522 | PL_comppad = newAV(); |
| 4523 | |
| 4524 | comppadlist = newAV(); |
| 4525 | AvREAL_off(comppadlist); |
| 4526 | av_store(comppadlist, 0, (SV*)PL_comppad_name); |
| 4527 | av_store(comppadlist, 1, (SV*)PL_comppad); |
| 4528 | CvPADLIST(cv) = comppadlist; |
| 4529 | av_fill(PL_comppad, AvFILLp(protopad)); |
| 4530 | PL_curpad = AvARRAY(PL_comppad); |
| 4531 | |
| 4532 | av = newAV(); /* will be @_ */ |
| 4533 | av_extend(av, 0); |
| 4534 | av_store(PL_comppad, 0, (SV*)av); |
| 4535 | AvFLAGS(av) = AVf_REIFY; |
| 4536 | |
| 4537 | for (ix = fpad; ix > 0; ix--) { |
| 4538 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
| 4539 | if (namesv && namesv != &PL_sv_undef) { |
| 4540 | char *name = SvPVX(namesv); /* XXX */ |
| 4541 | if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */ |
| 4542 | I32 off = pad_findlex(name, ix, SvIVX(namesv), |
| 4543 | CvOUTSIDE(cv), cxstack_ix, 0, 0); |
| 4544 | if (!off) |
| 4545 | PL_curpad[ix] = SvREFCNT_inc(ppad[ix]); |
| 4546 | else if (off != ix) |
| 4547 | Perl_croak(aTHX_ "panic: cv_clone: %s", name); |
| 4548 | } |
| 4549 | else { /* our own lexical */ |
| 4550 | SV* sv; |
| 4551 | if (*name == '&') { |
| 4552 | /* anon code -- we'll come back for it */ |
| 4553 | sv = SvREFCNT_inc(ppad[ix]); |
| 4554 | } |
| 4555 | else if (*name == '@') |
| 4556 | sv = (SV*)newAV(); |
| 4557 | else if (*name == '%') |
| 4558 | sv = (SV*)newHV(); |
| 4559 | else |
| 4560 | sv = NEWSV(0,0); |
| 4561 | if (!SvPADBUSY(sv)) |
| 4562 | SvPADMY_on(sv); |
| 4563 | PL_curpad[ix] = sv; |
| 4564 | } |
| 4565 | } |
| 4566 | else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) { |
| 4567 | PL_curpad[ix] = SvREFCNT_inc(ppad[ix]); |
| 4568 | } |
| 4569 | else { |
| 4570 | SV* sv = NEWSV(0,0); |
| 4571 | SvPADTMP_on(sv); |
| 4572 | PL_curpad[ix] = sv; |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | /* Now that vars are all in place, clone nested closures. */ |
| 4577 | |
| 4578 | for (ix = fpad; ix > 0; ix--) { |
| 4579 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
| 4580 | if (namesv |
| 4581 | && namesv != &PL_sv_undef |
| 4582 | && !(SvFLAGS(namesv) & SVf_FAKE) |
| 4583 | && *SvPVX(namesv) == '&' |
| 4584 | && CvCLONE(ppad[ix])) |
| 4585 | { |
| 4586 | CV *kid = cv_clone2((CV*)ppad[ix], cv); |
| 4587 | SvREFCNT_dec(ppad[ix]); |
| 4588 | CvCLONE_on(kid); |
| 4589 | SvPADMY_on(kid); |
| 4590 | PL_curpad[ix] = (SV*)kid; |
| 4591 | } |
| 4592 | } |
| 4593 | |
| 4594 | #ifdef DEBUG_CLOSURES |
| 4595 | PerlIO_printf(Perl_debug_log, "Cloned inside:\n"); |
| 4596 | cv_dump(outside); |
| 4597 | PerlIO_printf(Perl_debug_log, " from:\n"); |
| 4598 | cv_dump(proto); |
| 4599 | PerlIO_printf(Perl_debug_log, " to:\n"); |
| 4600 | cv_dump(cv); |
| 4601 | #endif |
| 4602 | |
| 4603 | LEAVE; |
| 4604 | |
| 4605 | if (CvCONST(cv)) { |
| 4606 | SV* const_sv = op_const_sv(CvSTART(cv), cv); |
| 4607 | assert(const_sv); |
| 4608 | /* constant sub () { $x } closing over $x - see lib/constant.pm */ |
| 4609 | SvREFCNT_dec(cv); |
| 4610 | cv = newCONSTSUB(CvSTASH(proto), 0, const_sv); |
| 4611 | } |
| 4612 | |
| 4613 | return cv; |
| 4614 | } |
| 4615 | |
| 4616 | CV * |
| 4617 | Perl_cv_clone(pTHX_ CV *proto) |
| 4618 | { |
| 4619 | CV *cv; |
| 4620 | LOCK_CRED_MUTEX; /* XXX create separate mutex */ |
| 4621 | cv = cv_clone2(proto, CvOUTSIDE(proto)); |
| 4622 | UNLOCK_CRED_MUTEX; /* XXX create separate mutex */ |
| 4623 | return cv; |
| 4624 | } |
| 4625 | |
| 4626 | void |
| 4627 | Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p) |
| 4628 | { |
| 4629 | if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) { |
| 4630 | SV* msg = sv_newmortal(); |
| 4631 | SV* name = Nullsv; |
| 4632 | |
| 4633 | if (gv) |
| 4634 | gv_efullname3(name = sv_newmortal(), gv, Nullch); |
| 4635 | sv_setpv(msg, "Prototype mismatch:"); |
| 4636 | if (name) |
| 4637 | Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name); |
| 4638 | if (SvPOK(cv)) |
| 4639 | Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv)); |
| 4640 | sv_catpv(msg, " vs "); |
| 4641 | if (p) |
| 4642 | Perl_sv_catpvf(aTHX_ msg, "(%s)", p); |
| 4643 | else |
| 4644 | sv_catpv(msg, "none"); |
| 4645 | Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg); |
| 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | static void const_sv_xsub(pTHX_ CV* cv); |
| 4650 | |
| 4651 | /* |
| 4652 | |
| 4653 | =head1 Optree Manipulation Functions |
| 4654 | |
| 4655 | =for apidoc cv_const_sv |
| 4656 | |
| 4657 | If C<cv> is a constant sub eligible for inlining. returns the constant |
| 4658 | value returned by the sub. Otherwise, returns NULL. |
| 4659 | |
| 4660 | Constant subs can be created with C<newCONSTSUB> or as described in |
| 4661 | L<perlsub/"Constant Functions">. |
| 4662 | |
| 4663 | =cut |
| 4664 | */ |
| 4665 | SV * |
| 4666 | Perl_cv_const_sv(pTHX_ CV *cv) |
| 4667 | { |
| 4668 | if (!cv || !CvCONST(cv)) |
| 4669 | return Nullsv; |
| 4670 | return (SV*)CvXSUBANY(cv).any_ptr; |
| 4671 | } |
| 4672 | |
| 4673 | SV * |
| 4674 | Perl_op_const_sv(pTHX_ OP *o, CV *cv) |
| 4675 | { |
| 4676 | SV *sv = Nullsv; |
| 4677 | |
| 4678 | if (!o) |
| 4679 | return Nullsv; |
| 4680 | |
| 4681 | if (o->op_type == OP_LINESEQ && cLISTOPo->op_first) |
| 4682 | o = cLISTOPo->op_first->op_sibling; |
| 4683 | |
| 4684 | for (; o; o = o->op_next) { |
| 4685 | OPCODE type = o->op_type; |
| 4686 | |
| 4687 | if (sv && o->op_next == o) |
| 4688 | return sv; |
| 4689 | if (o->op_next != o) { |
| 4690 | if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK) |
| 4691 | continue; |
| 4692 | if (type == OP_DBSTATE) |
| 4693 | continue; |
| 4694 | } |
| 4695 | if (type == OP_LEAVESUB || type == OP_RETURN) |
| 4696 | break; |
| 4697 | if (sv) |
| 4698 | return Nullsv; |
| 4699 | if (type == OP_CONST && cSVOPo->op_sv) |
| 4700 | sv = cSVOPo->op_sv; |
| 4701 | else if ((type == OP_PADSV || type == OP_CONST) && cv) { |
| 4702 | AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]); |
| 4703 | sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv; |
| 4704 | if (!sv) |
| 4705 | return Nullsv; |
| 4706 | if (CvCONST(cv)) { |
| 4707 | /* We get here only from cv_clone2() while creating a closure. |
| 4708 | Copy the const value here instead of in cv_clone2 so that |
| 4709 | SvREADONLY_on doesn't lead to problems when leaving |
| 4710 | scope. |
| 4711 | */ |
| 4712 | sv = newSVsv(sv); |
| 4713 | } |
| 4714 | if (!SvREADONLY(sv) && SvREFCNT(sv) > 1) |
| 4715 | return Nullsv; |
| 4716 | } |
| 4717 | else |
| 4718 | return Nullsv; |
| 4719 | } |
| 4720 | if (sv) |
| 4721 | SvREADONLY_on(sv); |
| 4722 | return sv; |
| 4723 | } |
| 4724 | |
| 4725 | void |
| 4726 | Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) |
| 4727 | { |
| 4728 | if (o) |
| 4729 | SAVEFREEOP(o); |
| 4730 | if (proto) |
| 4731 | SAVEFREEOP(proto); |
| 4732 | if (attrs) |
| 4733 | SAVEFREEOP(attrs); |
| 4734 | if (block) |
| 4735 | SAVEFREEOP(block); |
| 4736 | Perl_croak(aTHX_ "\"my sub\" not yet implemented"); |
| 4737 | } |
| 4738 | |
| 4739 | CV * |
| 4740 | Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block) |
| 4741 | { |
| 4742 | return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block); |
| 4743 | } |
| 4744 | |
| 4745 | CV * |
| 4746 | Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) |
| 4747 | { |
| 4748 | STRLEN n_a; |
| 4749 | char *name; |
| 4750 | char *aname; |
| 4751 | GV *gv; |
| 4752 | char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch; |
| 4753 | register CV *cv=0; |
| 4754 | I32 ix; |
| 4755 | SV *const_sv; |
| 4756 | |
| 4757 | name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch; |
| 4758 | if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) { |
| 4759 | SV *sv = sv_newmortal(); |
| 4760 | Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]", |
| 4761 | PL_curstash ? "__ANON__" : "__ANON__::__ANON__", |
| 4762 | CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); |
| 4763 | aname = SvPVX(sv); |
| 4764 | } |
| 4765 | else |
| 4766 | aname = Nullch; |
| 4767 | gv = gv_fetchpv(name ? name : (aname ? aname : |
| 4768 | (PL_curstash ? "__ANON__" : "__ANON__::__ANON__")), |
| 4769 | GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT), |
| 4770 | SVt_PVCV); |
| 4771 | |
| 4772 | if (o) |
| 4773 | SAVEFREEOP(o); |
| 4774 | if (proto) |
| 4775 | SAVEFREEOP(proto); |
| 4776 | if (attrs) |
| 4777 | SAVEFREEOP(attrs); |
| 4778 | |
| 4779 | if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at |
| 4780 | maximum a prototype before. */ |
| 4781 | if (SvTYPE(gv) > SVt_NULL) { |
| 4782 | if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1) |
| 4783 | && ckWARN_d(WARN_PROTOTYPE)) |
| 4784 | { |
| 4785 | Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype"); |
| 4786 | } |
| 4787 | cv_ckproto((CV*)gv, NULL, ps); |
| 4788 | } |
| 4789 | if (ps) |
| 4790 | sv_setpv((SV*)gv, ps); |
| 4791 | else |
| 4792 | sv_setiv((SV*)gv, -1); |
| 4793 | SvREFCNT_dec(PL_compcv); |
| 4794 | cv = PL_compcv = NULL; |
| 4795 | PL_sub_generation++; |
| 4796 | goto done; |
| 4797 | } |
| 4798 | |
| 4799 | cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv); |
| 4800 | |
| 4801 | #ifdef GV_UNIQUE_CHECK |
| 4802 | if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) { |
| 4803 | Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name); |
| 4804 | } |
| 4805 | #endif |
| 4806 | |
| 4807 | if (!block || !ps || *ps || attrs) |
| 4808 | const_sv = Nullsv; |
| 4809 | else |
| 4810 | const_sv = op_const_sv(block, Nullcv); |
| 4811 | |
| 4812 | if (cv) { |
| 4813 | bool exists = CvROOT(cv) || CvXSUB(cv); |
| 4814 | |
| 4815 | #ifdef GV_UNIQUE_CHECK |
| 4816 | if (exists && GvUNIQUE(gv)) { |
| 4817 | Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name); |
| 4818 | } |
| 4819 | #endif |
| 4820 | |
| 4821 | /* if the subroutine doesn't exist and wasn't pre-declared |
| 4822 | * with a prototype, assume it will be AUTOLOADed, |
| 4823 | * skipping the prototype check |
| 4824 | */ |
| 4825 | if (exists || SvPOK(cv)) |
| 4826 | cv_ckproto(cv, gv, ps); |
| 4827 | /* already defined (or promised)? */ |
| 4828 | if (exists || GvASSUMECV(gv)) { |
| 4829 | if (!block && !attrs) { |
| 4830 | if (CvFLAGS(PL_compcv)) { |
| 4831 | /* might have had built-in attrs applied */ |
| 4832 | CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS); |
| 4833 | } |
| 4834 | /* just a "sub foo;" when &foo is already defined */ |
| 4835 | SAVEFREESV(PL_compcv); |
| 4836 | goto done; |
| 4837 | } |
| 4838 | /* ahem, death to those who redefine active sort subs */ |
| 4839 | if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv)) |
| 4840 | Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name); |
| 4841 | if (block) { |
| 4842 | if (ckWARN(WARN_REDEFINE) |
| 4843 | || (CvCONST(cv) |
| 4844 | && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv)))) |
| 4845 | { |
| 4846 | line_t oldline = CopLINE(PL_curcop); |
| 4847 | if (PL_copline != NOLINE) |
| 4848 | CopLINE_set(PL_curcop, PL_copline); |
| 4849 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), |
| 4850 | CvCONST(cv) ? "Constant subroutine %s redefined" |
| 4851 | : "Subroutine %s redefined", name); |
| 4852 | CopLINE_set(PL_curcop, oldline); |
| 4853 | } |
| 4854 | SvREFCNT_dec(cv); |
| 4855 | cv = Nullcv; |
| 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | if (const_sv) { |
| 4860 | SvREFCNT_inc(const_sv); |
| 4861 | if (cv) { |
| 4862 | assert(!CvROOT(cv) && !CvCONST(cv)); |
| 4863 | sv_setpv((SV*)cv, ""); /* prototype is "" */ |
| 4864 | CvXSUBANY(cv).any_ptr = const_sv; |
| 4865 | CvXSUB(cv) = const_sv_xsub; |
| 4866 | CvCONST_on(cv); |
| 4867 | } |
| 4868 | else { |
| 4869 | GvCV(gv) = Nullcv; |
| 4870 | cv = newCONSTSUB(NULL, name, const_sv); |
| 4871 | } |
| 4872 | op_free(block); |
| 4873 | SvREFCNT_dec(PL_compcv); |
| 4874 | PL_compcv = NULL; |
| 4875 | PL_sub_generation++; |
| 4876 | goto done; |
| 4877 | } |
| 4878 | if (attrs) { |
| 4879 | HV *stash; |
| 4880 | SV *rcv; |
| 4881 | |
| 4882 | /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs> |
| 4883 | * before we clobber PL_compcv. |
| 4884 | */ |
| 4885 | if (cv && !block) { |
| 4886 | rcv = (SV*)cv; |
| 4887 | /* Might have had built-in attributes applied -- propagate them. */ |
| 4888 | CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS); |
| 4889 | if (CvGV(cv) && GvSTASH(CvGV(cv))) |
| 4890 | stash = GvSTASH(CvGV(cv)); |
| 4891 | else if (CvSTASH(cv)) |
| 4892 | stash = CvSTASH(cv); |
| 4893 | else |
| 4894 | stash = PL_curstash; |
| 4895 | } |
| 4896 | else { |
| 4897 | /* possibly about to re-define existing subr -- ignore old cv */ |
| 4898 | rcv = (SV*)PL_compcv; |
| 4899 | if (name && GvSTASH(gv)) |
| 4900 | stash = GvSTASH(gv); |
| 4901 | else |
| 4902 | stash = PL_curstash; |
| 4903 | } |
| 4904 | apply_attrs(stash, rcv, attrs, FALSE); |
| 4905 | } |
| 4906 | if (cv) { /* must reuse cv if autoloaded */ |
| 4907 | if (!block) { |
| 4908 | /* got here with just attrs -- work done, so bug out */ |
| 4909 | SAVEFREESV(PL_compcv); |
| 4910 | goto done; |
| 4911 | } |
| 4912 | cv_undef(cv); |
| 4913 | CvFLAGS(cv) = CvFLAGS(PL_compcv); |
| 4914 | CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv); |
| 4915 | CvOUTSIDE(PL_compcv) = 0; |
| 4916 | CvPADLIST(cv) = CvPADLIST(PL_compcv); |
| 4917 | CvPADLIST(PL_compcv) = 0; |
| 4918 | /* inner references to PL_compcv must be fixed up ... */ |
| 4919 | { |
| 4920 | AV *padlist = CvPADLIST(cv); |
| 4921 | AV *comppad_name = (AV*)AvARRAY(padlist)[0]; |
| 4922 | AV *comppad = (AV*)AvARRAY(padlist)[1]; |
| 4923 | SV **namepad = AvARRAY(comppad_name); |
| 4924 | SV **curpad = AvARRAY(comppad); |
| 4925 | for (ix = AvFILLp(comppad_name); ix > 0; ix--) { |
| 4926 | SV *namesv = namepad[ix]; |
| 4927 | if (namesv && namesv != &PL_sv_undef |
| 4928 | && *SvPVX(namesv) == '&') |
| 4929 | { |
| 4930 | CV *innercv = (CV*)curpad[ix]; |
| 4931 | if (CvOUTSIDE(innercv) == PL_compcv) { |
| 4932 | CvOUTSIDE(innercv) = cv; |
| 4933 | if (!CvANON(innercv) || CvCLONED(innercv)) { |
| 4934 | (void)SvREFCNT_inc(cv); |
| 4935 | SvREFCNT_dec(PL_compcv); |
| 4936 | } |
| 4937 | } |
| 4938 | } |
| 4939 | } |
| 4940 | } |
| 4941 | /* ... before we throw it away */ |
| 4942 | SvREFCNT_dec(PL_compcv); |
| 4943 | if (PERLDB_INTER)/* Advice debugger on the new sub. */ |
| 4944 | ++PL_sub_generation; |
| 4945 | } |
| 4946 | else { |
| 4947 | cv = PL_compcv; |
| 4948 | if (name) { |
| 4949 | GvCV(gv) = cv; |
| 4950 | GvCVGEN(gv) = 0; |
| 4951 | PL_sub_generation++; |
| 4952 | } |
| 4953 | } |
| 4954 | CvGV(cv) = gv; |
| 4955 | CvFILE_set_from_cop(cv, PL_curcop); |
| 4956 | CvSTASH(cv) = PL_curstash; |
| 4957 | #ifdef USE_5005THREADS |
| 4958 | CvOWNER(cv) = 0; |
| 4959 | if (!CvMUTEXP(cv)) { |
| 4960 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
| 4961 | MUTEX_INIT(CvMUTEXP(cv)); |
| 4962 | } |
| 4963 | #endif /* USE_5005THREADS */ |
| 4964 | |
| 4965 | if (ps) |
| 4966 | sv_setpv((SV*)cv, ps); |
| 4967 | |
| 4968 | if (PL_error_count) { |
| 4969 | op_free(block); |
| 4970 | block = Nullop; |
| 4971 | if (name) { |
| 4972 | char *s = strrchr(name, ':'); |
| 4973 | s = s ? s+1 : name; |
| 4974 | if (strEQ(s, "BEGIN")) { |
| 4975 | char *not_safe = |
| 4976 | "BEGIN not safe after errors--compilation aborted"; |
| 4977 | if (PL_in_eval & EVAL_KEEPERR) |
| 4978 | Perl_croak(aTHX_ not_safe); |
| 4979 | else { |
| 4980 | /* force display of errors found but not reported */ |
| 4981 | sv_catpv(ERRSV, not_safe); |
| 4982 | Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a)); |
| 4983 | } |
| 4984 | } |
| 4985 | } |
| 4986 | } |
| 4987 | if (!block) |
| 4988 | goto done; |
| 4989 | |
| 4990 | if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad)) |
| 4991 | av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv); |
| 4992 | |
| 4993 | if (CvLVALUE(cv)) { |
| 4994 | CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, |
| 4995 | mod(scalarseq(block), OP_LEAVESUBLV)); |
| 4996 | } |
| 4997 | else { |
| 4998 | CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block)); |
| 4999 | } |
| 5000 | CvROOT(cv)->op_private |= OPpREFCOUNTED; |
| 5001 | OpREFCNT_set(CvROOT(cv), 1); |
| 5002 | CvSTART(cv) = LINKLIST(CvROOT(cv)); |
| 5003 | CvROOT(cv)->op_next = 0; |
| 5004 | CALL_PEEP(CvSTART(cv)); |
| 5005 | |
| 5006 | /* now that optimizer has done its work, adjust pad values */ |
| 5007 | if (CvCLONE(cv)) { |
| 5008 | SV **namep = AvARRAY(PL_comppad_name); |
| 5009 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
| 5010 | SV *namesv; |
| 5011 | |
| 5012 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) |
| 5013 | continue; |
| 5014 | /* |
| 5015 | * The only things that a clonable function needs in its |
| 5016 | * pad are references to outer lexicals and anonymous subs. |
| 5017 | * The rest are created anew during cloning. |
| 5018 | */ |
| 5019 | if (!((namesv = namep[ix]) != Nullsv && |
| 5020 | namesv != &PL_sv_undef && |
| 5021 | (SvFAKE(namesv) || |
| 5022 | *SvPVX(namesv) == '&'))) |
| 5023 | { |
| 5024 | SvREFCNT_dec(PL_curpad[ix]); |
| 5025 | PL_curpad[ix] = Nullsv; |
| 5026 | } |
| 5027 | } |
| 5028 | assert(!CvCONST(cv)); |
| 5029 | if (ps && !*ps && op_const_sv(block, cv)) |
| 5030 | CvCONST_on(cv); |
| 5031 | } |
| 5032 | else { |
| 5033 | AV *av = newAV(); /* Will be @_ */ |
| 5034 | av_extend(av, 0); |
| 5035 | av_store(PL_comppad, 0, (SV*)av); |
| 5036 | AvFLAGS(av) = AVf_REIFY; |
| 5037 | |
| 5038 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
| 5039 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) |
| 5040 | continue; |
| 5041 | if (!SvPADMY(PL_curpad[ix])) |
| 5042 | SvPADTMP_on(PL_curpad[ix]); |
| 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | /* If a potential closure prototype, don't keep a refcount on outer CV. |
| 5047 | * This is okay as the lifetime of the prototype is tied to the |
| 5048 | * lifetime of the outer CV. Avoids memory leak due to reference |
| 5049 | * loop. --GSAR */ |
| 5050 | if (!name) |
| 5051 | SvREFCNT_dec(CvOUTSIDE(cv)); |
| 5052 | |
| 5053 | if (name || aname) { |
| 5054 | char *s; |
| 5055 | char *tname = (name ? name : aname); |
| 5056 | |
| 5057 | if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { |
| 5058 | SV *sv = NEWSV(0,0); |
| 5059 | SV *tmpstr = sv_newmortal(); |
| 5060 | GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV); |
| 5061 | CV *pcv; |
| 5062 | HV *hv; |
| 5063 | |
| 5064 | Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld", |
| 5065 | CopFILE(PL_curcop), |
| 5066 | (long)PL_subline, (long)CopLINE(PL_curcop)); |
| 5067 | gv_efullname3(tmpstr, gv, Nullch); |
| 5068 | hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0); |
| 5069 | hv = GvHVn(db_postponed); |
| 5070 | if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr)) |
| 5071 | && (pcv = GvCV(db_postponed))) |
| 5072 | { |
| 5073 | dSP; |
| 5074 | PUSHMARK(SP); |
| 5075 | XPUSHs(tmpstr); |
| 5076 | PUTBACK; |
| 5077 | call_sv((SV*)pcv, G_DISCARD); |
| 5078 | } |
| 5079 | } |
| 5080 | |
| 5081 | if ((s = strrchr(tname,':'))) |
| 5082 | s++; |
| 5083 | else |
| 5084 | s = tname; |
| 5085 | |
| 5086 | if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I') |
| 5087 | goto done; |
| 5088 | |
| 5089 | if (strEQ(s, "BEGIN")) { |
| 5090 | I32 oldscope = PL_scopestack_ix; |
| 5091 | ENTER; |
| 5092 | SAVECOPFILE(&PL_compiling); |
| 5093 | SAVECOPLINE(&PL_compiling); |
| 5094 | |
| 5095 | if (!PL_beginav) |
| 5096 | PL_beginav = newAV(); |
| 5097 | DEBUG_x( dump_sub(gv) ); |
| 5098 | av_push(PL_beginav, (SV*)cv); |
| 5099 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5100 | call_list(oldscope, PL_beginav); |
| 5101 | |
| 5102 | PL_curcop = &PL_compiling; |
| 5103 | PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK); |
| 5104 | LEAVE; |
| 5105 | } |
| 5106 | else if (strEQ(s, "END") && !PL_error_count) { |
| 5107 | if (!PL_endav) |
| 5108 | PL_endav = newAV(); |
| 5109 | DEBUG_x( dump_sub(gv) ); |
| 5110 | av_unshift(PL_endav, 1); |
| 5111 | av_store(PL_endav, 0, (SV*)cv); |
| 5112 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5113 | } |
| 5114 | else if (strEQ(s, "CHECK") && !PL_error_count) { |
| 5115 | if (!PL_checkav) |
| 5116 | PL_checkav = newAV(); |
| 5117 | DEBUG_x( dump_sub(gv) ); |
| 5118 | if (PL_main_start && ckWARN(WARN_VOID)) |
| 5119 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block"); |
| 5120 | av_unshift(PL_checkav, 1); |
| 5121 | av_store(PL_checkav, 0, (SV*)cv); |
| 5122 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5123 | } |
| 5124 | else if (strEQ(s, "INIT") && !PL_error_count) { |
| 5125 | if (!PL_initav) |
| 5126 | PL_initav = newAV(); |
| 5127 | DEBUG_x( dump_sub(gv) ); |
| 5128 | if (PL_main_start && ckWARN(WARN_VOID)) |
| 5129 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block"); |
| 5130 | av_push(PL_initav, (SV*)cv); |
| 5131 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5132 | } |
| 5133 | } |
| 5134 | |
| 5135 | done: |
| 5136 | PL_copline = NOLINE; |
| 5137 | LEAVE_SCOPE(floor); |
| 5138 | return cv; |
| 5139 | } |
| 5140 | |
| 5141 | /* XXX unsafe for threads if eval_owner isn't held */ |
| 5142 | /* |
| 5143 | =for apidoc newCONSTSUB |
| 5144 | |
| 5145 | Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is |
| 5146 | eligible for inlining at compile-time. |
| 5147 | |
| 5148 | =cut |
| 5149 | */ |
| 5150 | |
| 5151 | CV * |
| 5152 | Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv) |
| 5153 | { |
| 5154 | CV* cv; |
| 5155 | |
| 5156 | ENTER; |
| 5157 | |
| 5158 | SAVECOPLINE(PL_curcop); |
| 5159 | CopLINE_set(PL_curcop, PL_copline); |
| 5160 | |
| 5161 | SAVEHINTS(); |
| 5162 | PL_hints &= ~HINT_BLOCK_SCOPE; |
| 5163 | |
| 5164 | if (stash) { |
| 5165 | SAVESPTR(PL_curstash); |
| 5166 | SAVECOPSTASH(PL_curcop); |
| 5167 | PL_curstash = stash; |
| 5168 | CopSTASH_set(PL_curcop,stash); |
| 5169 | } |
| 5170 | |
| 5171 | cv = newXS(name, const_sv_xsub, __FILE__); |
| 5172 | CvXSUBANY(cv).any_ptr = sv; |
| 5173 | CvCONST_on(cv); |
| 5174 | sv_setpv((SV*)cv, ""); /* prototype is "" */ |
| 5175 | |
| 5176 | LEAVE; |
| 5177 | |
| 5178 | return cv; |
| 5179 | } |
| 5180 | |
| 5181 | /* |
| 5182 | =for apidoc U||newXS |
| 5183 | |
| 5184 | Used by C<xsubpp> to hook up XSUBs as Perl subs. |
| 5185 | |
| 5186 | =cut |
| 5187 | */ |
| 5188 | |
| 5189 | CV * |
| 5190 | Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename) |
| 5191 | { |
| 5192 | GV *gv = gv_fetchpv(name ? name : |
| 5193 | (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"), |
| 5194 | GV_ADDMULTI, SVt_PVCV); |
| 5195 | register CV *cv; |
| 5196 | |
| 5197 | if ((cv = (name ? GvCV(gv) : Nullcv))) { |
| 5198 | if (GvCVGEN(gv)) { |
| 5199 | /* just a cached method */ |
| 5200 | SvREFCNT_dec(cv); |
| 5201 | cv = 0; |
| 5202 | } |
| 5203 | else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { |
| 5204 | /* already defined (or promised) */ |
| 5205 | if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv)) |
| 5206 | && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) { |
| 5207 | line_t oldline = CopLINE(PL_curcop); |
| 5208 | if (PL_copline != NOLINE) |
| 5209 | CopLINE_set(PL_curcop, PL_copline); |
| 5210 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), |
| 5211 | CvCONST(cv) ? "Constant subroutine %s redefined" |
| 5212 | : "Subroutine %s redefined" |
| 5213 | ,name); |
| 5214 | CopLINE_set(PL_curcop, oldline); |
| 5215 | } |
| 5216 | SvREFCNT_dec(cv); |
| 5217 | cv = 0; |
| 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | if (cv) /* must reuse cv if autoloaded */ |
| 5222 | cv_undef(cv); |
| 5223 | else { |
| 5224 | cv = (CV*)NEWSV(1105,0); |
| 5225 | sv_upgrade((SV *)cv, SVt_PVCV); |
| 5226 | if (name) { |
| 5227 | GvCV(gv) = cv; |
| 5228 | GvCVGEN(gv) = 0; |
| 5229 | PL_sub_generation++; |
| 5230 | } |
| 5231 | } |
| 5232 | CvGV(cv) = gv; |
| 5233 | #ifdef USE_5005THREADS |
| 5234 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
| 5235 | MUTEX_INIT(CvMUTEXP(cv)); |
| 5236 | CvOWNER(cv) = 0; |
| 5237 | #endif /* USE_5005THREADS */ |
| 5238 | (void)gv_fetchfile(filename); |
| 5239 | CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be |
| 5240 | an external constant string */ |
| 5241 | CvXSUB(cv) = subaddr; |
| 5242 | |
| 5243 | if (name) { |
| 5244 | char *s = strrchr(name,':'); |
| 5245 | if (s) |
| 5246 | s++; |
| 5247 | else |
| 5248 | s = name; |
| 5249 | |
| 5250 | if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I') |
| 5251 | goto done; |
| 5252 | |
| 5253 | if (strEQ(s, "BEGIN")) { |
| 5254 | if (!PL_beginav) |
| 5255 | PL_beginav = newAV(); |
| 5256 | av_push(PL_beginav, (SV*)cv); |
| 5257 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5258 | } |
| 5259 | else if (strEQ(s, "END")) { |
| 5260 | if (!PL_endav) |
| 5261 | PL_endav = newAV(); |
| 5262 | av_unshift(PL_endav, 1); |
| 5263 | av_store(PL_endav, 0, (SV*)cv); |
| 5264 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5265 | } |
| 5266 | else if (strEQ(s, "CHECK")) { |
| 5267 | if (!PL_checkav) |
| 5268 | PL_checkav = newAV(); |
| 5269 | if (PL_main_start && ckWARN(WARN_VOID)) |
| 5270 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block"); |
| 5271 | av_unshift(PL_checkav, 1); |
| 5272 | av_store(PL_checkav, 0, (SV*)cv); |
| 5273 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5274 | } |
| 5275 | else if (strEQ(s, "INIT")) { |
| 5276 | if (!PL_initav) |
| 5277 | PL_initav = newAV(); |
| 5278 | if (PL_main_start && ckWARN(WARN_VOID)) |
| 5279 | Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block"); |
| 5280 | av_push(PL_initav, (SV*)cv); |
| 5281 | GvCV(gv) = 0; /* cv has been hijacked */ |
| 5282 | } |
| 5283 | } |
| 5284 | else |
| 5285 | CvANON_on(cv); |
| 5286 | |
| 5287 | done: |
| 5288 | return cv; |
| 5289 | } |
| 5290 | |
| 5291 | void |
| 5292 | Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block) |
| 5293 | { |
| 5294 | register CV *cv; |
| 5295 | char *name; |
| 5296 | GV *gv; |
| 5297 | I32 ix; |
| 5298 | STRLEN n_a; |
| 5299 | |
| 5300 | if (o) |
| 5301 | name = SvPVx(cSVOPo->op_sv, n_a); |
| 5302 | else |
| 5303 | name = "STDOUT"; |
| 5304 | gv = gv_fetchpv(name,TRUE, SVt_PVFM); |
| 5305 | #ifdef GV_UNIQUE_CHECK |
| 5306 | if (GvUNIQUE(gv)) { |
| 5307 | Perl_croak(aTHX_ "Bad symbol for form (GV is unique)"); |
| 5308 | } |
| 5309 | #endif |
| 5310 | GvMULTI_on(gv); |
| 5311 | if ((cv = GvFORM(gv))) { |
| 5312 | if (ckWARN(WARN_REDEFINE)) { |
| 5313 | line_t oldline = CopLINE(PL_curcop); |
| 5314 | if (PL_copline != NOLINE) |
| 5315 | CopLINE_set(PL_curcop, PL_copline); |
| 5316 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Format %s redefined",name); |
| 5317 | CopLINE_set(PL_curcop, oldline); |
| 5318 | } |
| 5319 | SvREFCNT_dec(cv); |
| 5320 | } |
| 5321 | cv = PL_compcv; |
| 5322 | GvFORM(gv) = cv; |
| 5323 | CvGV(cv) = gv; |
| 5324 | CvFILE_set_from_cop(cv, PL_curcop); |
| 5325 | |
| 5326 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
| 5327 | if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix])) |
| 5328 | SvPADTMP_on(PL_curpad[ix]); |
| 5329 | } |
| 5330 | |
| 5331 | CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); |
| 5332 | CvROOT(cv)->op_private |= OPpREFCOUNTED; |
| 5333 | OpREFCNT_set(CvROOT(cv), 1); |
| 5334 | CvSTART(cv) = LINKLIST(CvROOT(cv)); |
| 5335 | CvROOT(cv)->op_next = 0; |
| 5336 | CALL_PEEP(CvSTART(cv)); |
| 5337 | op_free(o); |
| 5338 | PL_copline = NOLINE; |
| 5339 | LEAVE_SCOPE(floor); |
| 5340 | } |
| 5341 | |
| 5342 | OP * |
| 5343 | Perl_newANONLIST(pTHX_ OP *o) |
| 5344 | { |
| 5345 | return newUNOP(OP_REFGEN, 0, |
| 5346 | mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN)); |
| 5347 | } |
| 5348 | |
| 5349 | OP * |
| 5350 | Perl_newANONHASH(pTHX_ OP *o) |
| 5351 | { |
| 5352 | return newUNOP(OP_REFGEN, 0, |
| 5353 | mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN)); |
| 5354 | } |
| 5355 | |
| 5356 | OP * |
| 5357 | Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block) |
| 5358 | { |
| 5359 | return newANONATTRSUB(floor, proto, Nullop, block); |
| 5360 | } |
| 5361 | |
| 5362 | OP * |
| 5363 | Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block) |
| 5364 | { |
| 5365 | return newUNOP(OP_REFGEN, 0, |
| 5366 | newSVOP(OP_ANONCODE, 0, |
| 5367 | (SV*)newATTRSUB(floor, 0, proto, attrs, block))); |
| 5368 | } |
| 5369 | |
| 5370 | OP * |
| 5371 | Perl_oopsAV(pTHX_ OP *o) |
| 5372 | { |
| 5373 | switch (o->op_type) { |
| 5374 | case OP_PADSV: |
| 5375 | o->op_type = OP_PADAV; |
| 5376 | o->op_ppaddr = PL_ppaddr[OP_PADAV]; |
| 5377 | return ref(o, OP_RV2AV); |
| 5378 | |
| 5379 | case OP_RV2SV: |
| 5380 | o->op_type = OP_RV2AV; |
| 5381 | o->op_ppaddr = PL_ppaddr[OP_RV2AV]; |
| 5382 | ref(o, OP_RV2AV); |
| 5383 | break; |
| 5384 | |
| 5385 | default: |
| 5386 | if (ckWARN_d(WARN_INTERNAL)) |
| 5387 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV"); |
| 5388 | break; |
| 5389 | } |
| 5390 | return o; |
| 5391 | } |
| 5392 | |
| 5393 | OP * |
| 5394 | Perl_oopsHV(pTHX_ OP *o) |
| 5395 | { |
| 5396 | switch (o->op_type) { |
| 5397 | case OP_PADSV: |
| 5398 | case OP_PADAV: |
| 5399 | o->op_type = OP_PADHV; |
| 5400 | o->op_ppaddr = PL_ppaddr[OP_PADHV]; |
| 5401 | return ref(o, OP_RV2HV); |
| 5402 | |
| 5403 | case OP_RV2SV: |
| 5404 | case OP_RV2AV: |
| 5405 | o->op_type = OP_RV2HV; |
| 5406 | o->op_ppaddr = PL_ppaddr[OP_RV2HV]; |
| 5407 | ref(o, OP_RV2HV); |
| 5408 | break; |
| 5409 | |
| 5410 | default: |
| 5411 | if (ckWARN_d(WARN_INTERNAL)) |
| 5412 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV"); |
| 5413 | break; |
| 5414 | } |
| 5415 | return o; |
| 5416 | } |
| 5417 | |
| 5418 | OP * |
| 5419 | Perl_newAVREF(pTHX_ OP *o) |
| 5420 | { |
| 5421 | if (o->op_type == OP_PADANY) { |
| 5422 | o->op_type = OP_PADAV; |
| 5423 | o->op_ppaddr = PL_ppaddr[OP_PADAV]; |
| 5424 | return o; |
| 5425 | } |
| 5426 | else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV) |
| 5427 | && ckWARN(WARN_DEPRECATED)) { |
| 5428 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), |
| 5429 | "Using an array as a reference is deprecated"); |
| 5430 | } |
| 5431 | return newUNOP(OP_RV2AV, 0, scalar(o)); |
| 5432 | } |
| 5433 | |
| 5434 | OP * |
| 5435 | Perl_newGVREF(pTHX_ I32 type, OP *o) |
| 5436 | { |
| 5437 | if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT) |
| 5438 | return newUNOP(OP_NULL, 0, o); |
| 5439 | return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); |
| 5440 | } |
| 5441 | |
| 5442 | OP * |
| 5443 | Perl_newHVREF(pTHX_ OP *o) |
| 5444 | { |
| 5445 | if (o->op_type == OP_PADANY) { |
| 5446 | o->op_type = OP_PADHV; |
| 5447 | o->op_ppaddr = PL_ppaddr[OP_PADHV]; |
| 5448 | return o; |
| 5449 | } |
| 5450 | else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV) |
| 5451 | && ckWARN(WARN_DEPRECATED)) { |
| 5452 | Perl_warner(aTHX_ packWARN(WARN_DEPRECATED), |
| 5453 | "Using a hash as a reference is deprecated"); |
| 5454 | } |
| 5455 | return newUNOP(OP_RV2HV, 0, scalar(o)); |
| 5456 | } |
| 5457 | |
| 5458 | OP * |
| 5459 | Perl_oopsCV(pTHX_ OP *o) |
| 5460 | { |
| 5461 | Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__); |
| 5462 | /* STUB */ |
| 5463 | return o; |
| 5464 | } |
| 5465 | |
| 5466 | OP * |
| 5467 | Perl_newCVREF(pTHX_ I32 flags, OP *o) |
| 5468 | { |
| 5469 | return newUNOP(OP_RV2CV, flags, scalar(o)); |
| 5470 | } |
| 5471 | |
| 5472 | OP * |
| 5473 | Perl_newSVREF(pTHX_ OP *o) |
| 5474 | { |
| 5475 | if (o->op_type == OP_PADANY) { |
| 5476 | o->op_type = OP_PADSV; |
| 5477 | o->op_ppaddr = PL_ppaddr[OP_PADSV]; |
| 5478 | return o; |
| 5479 | } |
| 5480 | else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) { |
| 5481 | o->op_flags |= OPpDONE_SVREF; |
| 5482 | return o; |
| 5483 | } |
| 5484 | return newUNOP(OP_RV2SV, 0, scalar(o)); |
| 5485 | } |
| 5486 | |
| 5487 | /* Check routines. */ |
| 5488 | |
| 5489 | OP * |
| 5490 | Perl_ck_anoncode(pTHX_ OP *o) |
| 5491 | { |
| 5492 | PADOFFSET ix; |
| 5493 | SV* name; |
| 5494 | |
| 5495 | name = NEWSV(1106,0); |
| 5496 | sv_upgrade(name, SVt_PVNV); |
| 5497 | sv_setpvn(name, "&", 1); |
| 5498 | SvIVX(name) = -1; |
| 5499 | SvNVX(name) = 1; |
| 5500 | ix = pad_alloc(o->op_type, SVs_PADMY); |
| 5501 | av_store(PL_comppad_name, ix, name); |
| 5502 | av_store(PL_comppad, ix, cSVOPo->op_sv); |
| 5503 | SvPADMY_on(cSVOPo->op_sv); |
| 5504 | cSVOPo->op_sv = Nullsv; |
| 5505 | cSVOPo->op_targ = ix; |
| 5506 | return o; |
| 5507 | } |
| 5508 | |
| 5509 | OP * |
| 5510 | Perl_ck_bitop(pTHX_ OP *o) |
| 5511 | { |
| 5512 | o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK); |
| 5513 | return o; |
| 5514 | } |
| 5515 | |
| 5516 | OP * |
| 5517 | Perl_ck_concat(pTHX_ OP *o) |
| 5518 | { |
| 5519 | if (cUNOPo->op_first->op_type == OP_CONCAT) |
| 5520 | o->op_flags |= OPf_STACKED; |
| 5521 | return o; |
| 5522 | } |
| 5523 | |
| 5524 | OP * |
| 5525 | Perl_ck_spair(pTHX_ OP *o) |
| 5526 | { |
| 5527 | if (o->op_flags & OPf_KIDS) { |
| 5528 | OP* newop; |
| 5529 | OP* kid; |
| 5530 | OPCODE type = o->op_type; |
| 5531 | o = modkids(ck_fun(o), type); |
| 5532 | kid = cUNOPo->op_first; |
| 5533 | newop = kUNOP->op_first->op_sibling; |
| 5534 | if (newop && |
| 5535 | (newop->op_sibling || |
| 5536 | !(PL_opargs[newop->op_type] & OA_RETSCALAR) || |
| 5537 | newop->op_type == OP_PADAV || newop->op_type == OP_PADHV || |
| 5538 | newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) { |
| 5539 | |
| 5540 | return o; |
| 5541 | } |
| 5542 | op_free(kUNOP->op_first); |
| 5543 | kUNOP->op_first = newop; |
| 5544 | } |
| 5545 | o->op_ppaddr = PL_ppaddr[++o->op_type]; |
| 5546 | return ck_fun(o); |
| 5547 | } |
| 5548 | |
| 5549 | OP * |
| 5550 | Perl_ck_delete(pTHX_ OP *o) |
| 5551 | { |
| 5552 | o = ck_fun(o); |
| 5553 | o->op_private = 0; |
| 5554 | if (o->op_flags & OPf_KIDS) { |
| 5555 | OP *kid = cUNOPo->op_first; |
| 5556 | switch (kid->op_type) { |
| 5557 | case OP_ASLICE: |
| 5558 | o->op_flags |= OPf_SPECIAL; |
| 5559 | /* FALL THROUGH */ |
| 5560 | case OP_HSLICE: |
| 5561 | o->op_private |= OPpSLICE; |
| 5562 | break; |
| 5563 | case OP_AELEM: |
| 5564 | o->op_flags |= OPf_SPECIAL; |
| 5565 | /* FALL THROUGH */ |
| 5566 | case OP_HELEM: |
| 5567 | break; |
| 5568 | default: |
| 5569 | Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice", |
| 5570 | OP_DESC(o)); |
| 5571 | } |
| 5572 | op_null(kid); |
| 5573 | } |
| 5574 | return o; |
| 5575 | } |
| 5576 | |
| 5577 | OP * |
| 5578 | Perl_ck_die(pTHX_ OP *o) |
| 5579 | { |
| 5580 | #ifdef VMS |
| 5581 | if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH; |
| 5582 | #endif |
| 5583 | return ck_fun(o); |
| 5584 | } |
| 5585 | |
| 5586 | OP * |
| 5587 | Perl_ck_eof(pTHX_ OP *o) |
| 5588 | { |
| 5589 | I32 type = o->op_type; |
| 5590 | |
| 5591 | if (o->op_flags & OPf_KIDS) { |
| 5592 | if (cLISTOPo->op_first->op_type == OP_STUB) { |
| 5593 | op_free(o); |
| 5594 | o = newUNOP(type, OPf_SPECIAL, |
| 5595 | newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV))); |
| 5596 | } |
| 5597 | return ck_fun(o); |
| 5598 | } |
| 5599 | return o; |
| 5600 | } |
| 5601 | |
| 5602 | OP * |
| 5603 | Perl_ck_eval(pTHX_ OP *o) |
| 5604 | { |
| 5605 | PL_hints |= HINT_BLOCK_SCOPE; |
| 5606 | if (o->op_flags & OPf_KIDS) { |
| 5607 | SVOP *kid = (SVOP*)cUNOPo->op_first; |
| 5608 | |
| 5609 | if (!kid) { |
| 5610 | o->op_flags &= ~OPf_KIDS; |
| 5611 | op_null(o); |
| 5612 | } |
| 5613 | else if (kid->op_type == OP_LINESEQ) { |
| 5614 | LOGOP *enter; |
| 5615 | |
| 5616 | kid->op_next = o->op_next; |
| 5617 | cUNOPo->op_first = 0; |
| 5618 | op_free(o); |
| 5619 | |
| 5620 | NewOp(1101, enter, 1, LOGOP); |
| 5621 | enter->op_type = OP_ENTERTRY; |
| 5622 | enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY]; |
| 5623 | enter->op_private = 0; |
| 5624 | |
| 5625 | /* establish postfix order */ |
| 5626 | enter->op_next = (OP*)enter; |
| 5627 | |
| 5628 | o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); |
| 5629 | o->op_type = OP_LEAVETRY; |
| 5630 | o->op_ppaddr = PL_ppaddr[OP_LEAVETRY]; |
| 5631 | enter->op_other = o; |
| 5632 | return o; |
| 5633 | } |
| 5634 | else |
| 5635 | scalar((OP*)kid); |
| 5636 | } |
| 5637 | else { |
| 5638 | op_free(o); |
| 5639 | o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP()); |
| 5640 | } |
| 5641 | o->op_targ = (PADOFFSET)PL_hints; |
| 5642 | return o; |
| 5643 | } |
| 5644 | |
| 5645 | OP * |
| 5646 | Perl_ck_exit(pTHX_ OP *o) |
| 5647 | { |
| 5648 | #ifdef VMS |
| 5649 | HV *table = GvHV(PL_hintgv); |
| 5650 | if (table) { |
| 5651 | SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE); |
| 5652 | if (svp && *svp && SvTRUE(*svp)) |
| 5653 | o->op_private |= OPpEXIT_VMSISH; |
| 5654 | } |
| 5655 | if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH; |
| 5656 | #endif |
| 5657 | return ck_fun(o); |
| 5658 | } |
| 5659 | |
| 5660 | OP * |
| 5661 | Perl_ck_exec(pTHX_ OP *o) |
| 5662 | { |
| 5663 | OP *kid; |
| 5664 | if (o->op_flags & OPf_STACKED) { |
| 5665 | o = ck_fun(o); |
| 5666 | kid = cUNOPo->op_first->op_sibling; |
| 5667 | if (kid->op_type == OP_RV2GV) |
| 5668 | op_null(kid); |
| 5669 | } |
| 5670 | else |
| 5671 | o = listkids(o); |
| 5672 | return o; |
| 5673 | } |
| 5674 | |
| 5675 | OP * |
| 5676 | Perl_ck_exists(pTHX_ OP *o) |
| 5677 | { |
| 5678 | o = ck_fun(o); |
| 5679 | if (o->op_flags & OPf_KIDS) { |
| 5680 | OP *kid = cUNOPo->op_first; |
| 5681 | if (kid->op_type == OP_ENTERSUB) { |
| 5682 | (void) ref(kid, o->op_type); |
| 5683 | if (kid->op_type != OP_RV2CV && !PL_error_count) |
| 5684 | Perl_croak(aTHX_ "%s argument is not a subroutine name", |
| 5685 | OP_DESC(o)); |
| 5686 | o->op_private |= OPpEXISTS_SUB; |
| 5687 | } |
| 5688 | else if (kid->op_type == OP_AELEM) |
| 5689 | o->op_flags |= OPf_SPECIAL; |
| 5690 | else if (kid->op_type != OP_HELEM) |
| 5691 | Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element", |
| 5692 | OP_DESC(o)); |
| 5693 | op_null(kid); |
| 5694 | } |
| 5695 | return o; |
| 5696 | } |
| 5697 | |
| 5698 | #if 0 |
| 5699 | OP * |
| 5700 | Perl_ck_gvconst(pTHX_ register OP *o) |
| 5701 | { |
| 5702 | o = fold_constants(o); |
| 5703 | if (o->op_type == OP_CONST) |
| 5704 | o->op_type = OP_GV; |
| 5705 | return o; |
| 5706 | } |
| 5707 | #endif |
| 5708 | |
| 5709 | OP * |
| 5710 | Perl_ck_rvconst(pTHX_ register OP *o) |
| 5711 | { |
| 5712 | SVOP *kid = (SVOP*)cUNOPo->op_first; |
| 5713 | |
| 5714 | o->op_private |= (PL_hints & HINT_STRICT_REFS); |
| 5715 | if (kid->op_type == OP_CONST) { |
| 5716 | char *name; |
| 5717 | int iscv; |
| 5718 | GV *gv; |
| 5719 | SV *kidsv = kid->op_sv; |
| 5720 | STRLEN n_a; |
| 5721 | |
| 5722 | /* Is it a constant from cv_const_sv()? */ |
| 5723 | if (SvROK(kidsv) && SvREADONLY(kidsv)) { |
| 5724 | SV *rsv = SvRV(kidsv); |
| 5725 | int svtype = SvTYPE(rsv); |
| 5726 | char *badtype = Nullch; |
| 5727 | |
| 5728 | switch (o->op_type) { |
| 5729 | case OP_RV2SV: |
| 5730 | if (svtype > SVt_PVMG) |
| 5731 | badtype = "a SCALAR"; |
| 5732 | break; |
| 5733 | case OP_RV2AV: |
| 5734 | if (svtype != SVt_PVAV) |
| 5735 | badtype = "an ARRAY"; |
| 5736 | break; |
| 5737 | case OP_RV2HV: |
| 5738 | if (svtype != SVt_PVHV) { |
| 5739 | if (svtype == SVt_PVAV) { /* pseudohash? */ |
| 5740 | SV **ksv = av_fetch((AV*)rsv, 0, FALSE); |
| 5741 | if (ksv && SvROK(*ksv) |
| 5742 | && SvTYPE(SvRV(*ksv)) == SVt_PVHV) |
| 5743 | { |
| 5744 | break; |
| 5745 | } |
| 5746 | } |
| 5747 | badtype = "a HASH"; |
| 5748 | } |
| 5749 | break; |
| 5750 | case OP_RV2CV: |
| 5751 | if (svtype != SVt_PVCV) |
| 5752 | badtype = "a CODE"; |
| 5753 | break; |
| 5754 | } |
| 5755 | if (badtype) |
| 5756 | Perl_croak(aTHX_ "Constant is not %s reference", badtype); |
| 5757 | return o; |
| 5758 | } |
| 5759 | name = SvPV(kidsv, n_a); |
| 5760 | if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { |
| 5761 | char *badthing = Nullch; |
| 5762 | switch (o->op_type) { |
| 5763 | case OP_RV2SV: |
| 5764 | badthing = "a SCALAR"; |
| 5765 | break; |
| 5766 | case OP_RV2AV: |
| 5767 | badthing = "an ARRAY"; |
| 5768 | break; |
| 5769 | case OP_RV2HV: |
| 5770 | badthing = "a HASH"; |
| 5771 | break; |
| 5772 | } |
| 5773 | if (badthing) |
| 5774 | Perl_croak(aTHX_ |
| 5775 | "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use", |
| 5776 | name, badthing); |
| 5777 | } |
| 5778 | /* |
| 5779 | * This is a little tricky. We only want to add the symbol if we |
| 5780 | * didn't add it in the lexer. Otherwise we get duplicate strict |
| 5781 | * warnings. But if we didn't add it in the lexer, we must at |
| 5782 | * least pretend like we wanted to add it even if it existed before, |
| 5783 | * or we get possible typo warnings. OPpCONST_ENTERED says |
| 5784 | * whether the lexer already added THIS instance of this symbol. |
| 5785 | */ |
| 5786 | iscv = (o->op_type == OP_RV2CV) * 2; |
| 5787 | do { |
| 5788 | gv = gv_fetchpv(name, |
| 5789 | iscv | !(kid->op_private & OPpCONST_ENTERED), |
| 5790 | iscv |
| 5791 | ? SVt_PVCV |
| 5792 | : o->op_type == OP_RV2SV |
| 5793 | ? SVt_PV |
| 5794 | : o->op_type == OP_RV2AV |
| 5795 | ? SVt_PVAV |
| 5796 | : o->op_type == OP_RV2HV |
| 5797 | ? SVt_PVHV |
| 5798 | : SVt_PVGV); |
| 5799 | } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++); |
| 5800 | if (gv) { |
| 5801 | kid->op_type = OP_GV; |
| 5802 | SvREFCNT_dec(kid->op_sv); |
| 5803 | #ifdef USE_ITHREADS |
| 5804 | /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */ |
| 5805 | kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP); |
| 5806 | SvREFCNT_dec(PL_curpad[kPADOP->op_padix]); |
| 5807 | GvIN_PAD_on(gv); |
| 5808 | PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv); |
| 5809 | #else |
| 5810 | kid->op_sv = SvREFCNT_inc(gv); |
| 5811 | #endif |
| 5812 | kid->op_private = 0; |
| 5813 | kid->op_ppaddr = PL_ppaddr[OP_GV]; |
| 5814 | } |
| 5815 | } |
| 5816 | return o; |
| 5817 | } |
| 5818 | |
| 5819 | OP * |
| 5820 | Perl_ck_ftst(pTHX_ OP *o) |
| 5821 | { |
| 5822 | I32 type = o->op_type; |
| 5823 | |
| 5824 | if (o->op_flags & OPf_REF) { |
| 5825 | /* nothing */ |
| 5826 | } |
| 5827 | else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) { |
| 5828 | SVOP *kid = (SVOP*)cUNOPo->op_first; |
| 5829 | |
| 5830 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { |
| 5831 | STRLEN n_a; |
| 5832 | OP *newop = newGVOP(type, OPf_REF, |
| 5833 | gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO)); |
| 5834 | op_free(o); |
| 5835 | o = newop; |
| 5836 | } |
| 5837 | } |
| 5838 | else { |
| 5839 | op_free(o); |
| 5840 | if (type == OP_FTTTY) |
| 5841 | o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE, |
| 5842 | SVt_PVIO)); |
| 5843 | else |
| 5844 | o = newUNOP(type, 0, newDEFSVOP()); |
| 5845 | } |
| 5846 | return o; |
| 5847 | } |
| 5848 | |
| 5849 | OP * |
| 5850 | Perl_ck_fun(pTHX_ OP *o) |
| 5851 | { |
| 5852 | register OP *kid; |
| 5853 | OP **tokid; |
| 5854 | OP *sibl; |
| 5855 | I32 numargs = 0; |
| 5856 | int type = o->op_type; |
| 5857 | register I32 oa = PL_opargs[type] >> OASHIFT; |
| 5858 | |
| 5859 | if (o->op_flags & OPf_STACKED) { |
| 5860 | if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) |
| 5861 | oa &= ~OA_OPTIONAL; |
| 5862 | else |
| 5863 | return no_fh_allowed(o); |
| 5864 | } |
| 5865 | |
| 5866 | if (o->op_flags & OPf_KIDS) { |
| 5867 | STRLEN n_a; |
| 5868 | tokid = &cLISTOPo->op_first; |
| 5869 | kid = cLISTOPo->op_first; |
| 5870 | if (kid->op_type == OP_PUSHMARK || |
| 5871 | (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)) |
| 5872 | { |
| 5873 | tokid = &kid->op_sibling; |
| 5874 | kid = kid->op_sibling; |
| 5875 | } |
| 5876 | if (!kid && PL_opargs[type] & OA_DEFGV) |
| 5877 | *tokid = kid = newDEFSVOP(); |
| 5878 | |
| 5879 | while (oa && kid) { |
| 5880 | numargs++; |
| 5881 | sibl = kid->op_sibling; |
| 5882 | switch (oa & 7) { |
| 5883 | case OA_SCALAR: |
| 5884 | /* list seen where single (scalar) arg expected? */ |
| 5885 | if (numargs == 1 && !(oa >> 4) |
| 5886 | && kid->op_type == OP_LIST && type != OP_SCALAR) |
| 5887 | { |
| 5888 | return too_many_arguments(o,PL_op_desc[type]); |
| 5889 | } |
| 5890 | scalar(kid); |
| 5891 | break; |
| 5892 | case OA_LIST: |
| 5893 | if (oa < 16) { |
| 5894 | kid = 0; |
| 5895 | continue; |
| 5896 | } |
| 5897 | else |
| 5898 | list(kid); |
| 5899 | break; |
| 5900 | case OA_AVREF: |
| 5901 | if ((type == OP_PUSH || type == OP_UNSHIFT) |
| 5902 | && !kid->op_sibling && ckWARN(WARN_SYNTAX)) |
| 5903 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), |
| 5904 | "Useless use of %s with no values", |
| 5905 | PL_op_desc[type]); |
| 5906 | |
| 5907 | if (kid->op_type == OP_CONST && |
| 5908 | (kid->op_private & OPpCONST_BARE)) |
| 5909 | { |
| 5910 | char *name = SvPVx(((SVOP*)kid)->op_sv, n_a); |
| 5911 | OP *newop = newAVREF(newGVOP(OP_GV, 0, |
| 5912 | gv_fetchpv(name, TRUE, SVt_PVAV) )); |
| 5913 | if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) |
| 5914 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 5915 | "Array @%s missing the @ in argument %"IVdf" of %s()", |
| 5916 | name, (IV)numargs, PL_op_desc[type]); |
| 5917 | op_free(kid); |
| 5918 | kid = newop; |
| 5919 | kid->op_sibling = sibl; |
| 5920 | *tokid = kid; |
| 5921 | } |
| 5922 | else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) |
| 5923 | bad_type(numargs, "array", PL_op_desc[type], kid); |
| 5924 | mod(kid, type); |
| 5925 | break; |
| 5926 | case OA_HVREF: |
| 5927 | if (kid->op_type == OP_CONST && |
| 5928 | (kid->op_private & OPpCONST_BARE)) |
| 5929 | { |
| 5930 | char *name = SvPVx(((SVOP*)kid)->op_sv, n_a); |
| 5931 | OP *newop = newHVREF(newGVOP(OP_GV, 0, |
| 5932 | gv_fetchpv(name, TRUE, SVt_PVHV) )); |
| 5933 | if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) |
| 5934 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 5935 | "Hash %%%s missing the %% in argument %"IVdf" of %s()", |
| 5936 | name, (IV)numargs, PL_op_desc[type]); |
| 5937 | op_free(kid); |
| 5938 | kid = newop; |
| 5939 | kid->op_sibling = sibl; |
| 5940 | *tokid = kid; |
| 5941 | } |
| 5942 | else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) |
| 5943 | bad_type(numargs, "hash", PL_op_desc[type], kid); |
| 5944 | mod(kid, type); |
| 5945 | break; |
| 5946 | case OA_CVREF: |
| 5947 | { |
| 5948 | OP *newop = newUNOP(OP_NULL, 0, kid); |
| 5949 | kid->op_sibling = 0; |
| 5950 | linklist(kid); |
| 5951 | newop->op_next = newop; |
| 5952 | kid = newop; |
| 5953 | kid->op_sibling = sibl; |
| 5954 | *tokid = kid; |
| 5955 | } |
| 5956 | break; |
| 5957 | case OA_FILEREF: |
| 5958 | if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) { |
| 5959 | if (kid->op_type == OP_CONST && |
| 5960 | (kid->op_private & OPpCONST_BARE)) |
| 5961 | { |
| 5962 | OP *newop = newGVOP(OP_GV, 0, |
| 5963 | gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE, |
| 5964 | SVt_PVIO) ); |
| 5965 | if (!(o->op_private & 1) && /* if not unop */ |
| 5966 | kid == cLISTOPo->op_last) |
| 5967 | cLISTOPo->op_last = newop; |
| 5968 | op_free(kid); |
| 5969 | kid = newop; |
| 5970 | } |
| 5971 | else if (kid->op_type == OP_READLINE) { |
| 5972 | /* neophyte patrol: open(<FH>), close(<FH>) etc. */ |
| 5973 | bad_type(numargs, "HANDLE", OP_DESC(o), kid); |
| 5974 | } |
| 5975 | else { |
| 5976 | I32 flags = OPf_SPECIAL; |
| 5977 | I32 priv = 0; |
| 5978 | PADOFFSET targ = 0; |
| 5979 | |
| 5980 | /* is this op a FH constructor? */ |
| 5981 | if (is_handle_constructor(o,numargs)) { |
| 5982 | char *name = Nullch; |
| 5983 | STRLEN len; |
| 5984 | |
| 5985 | flags = 0; |
| 5986 | /* Set a flag to tell rv2gv to vivify |
| 5987 | * need to "prove" flag does not mean something |
| 5988 | * else already - NI-S 1999/05/07 |
| 5989 | */ |
| 5990 | priv = OPpDEREF; |
| 5991 | if (kid->op_type == OP_PADSV) { |
| 5992 | SV **namep = av_fetch(PL_comppad_name, |
| 5993 | kid->op_targ, 4); |
| 5994 | if (namep && *namep) |
| 5995 | name = SvPV(*namep, len); |
| 5996 | } |
| 5997 | else if (kid->op_type == OP_RV2SV |
| 5998 | && kUNOP->op_first->op_type == OP_GV) |
| 5999 | { |
| 6000 | GV *gv = cGVOPx_gv(kUNOP->op_first); |
| 6001 | name = GvNAME(gv); |
| 6002 | len = GvNAMELEN(gv); |
| 6003 | } |
| 6004 | else if (kid->op_type == OP_AELEM |
| 6005 | || kid->op_type == OP_HELEM) |
| 6006 | { |
| 6007 | name = "__ANONIO__"; |
| 6008 | len = 10; |
| 6009 | mod(kid,type); |
| 6010 | } |
| 6011 | if (name) { |
| 6012 | SV *namesv; |
| 6013 | targ = pad_alloc(OP_RV2GV, SVs_PADTMP); |
| 6014 | namesv = PL_curpad[targ]; |
| 6015 | (void)SvUPGRADE(namesv, SVt_PV); |
| 6016 | if (*name != '$') |
| 6017 | sv_setpvn(namesv, "$", 1); |
| 6018 | sv_catpvn(namesv, name, len); |
| 6019 | } |
| 6020 | } |
| 6021 | kid->op_sibling = 0; |
| 6022 | kid = newUNOP(OP_RV2GV, flags, scalar(kid)); |
| 6023 | kid->op_targ = targ; |
| 6024 | kid->op_private |= priv; |
| 6025 | } |
| 6026 | kid->op_sibling = sibl; |
| 6027 | *tokid = kid; |
| 6028 | } |
| 6029 | scalar(kid); |
| 6030 | break; |
| 6031 | case OA_SCALARREF: |
| 6032 | mod(scalar(kid), type); |
| 6033 | break; |
| 6034 | } |
| 6035 | oa >>= 4; |
| 6036 | tokid = &kid->op_sibling; |
| 6037 | kid = kid->op_sibling; |
| 6038 | } |
| 6039 | o->op_private |= numargs; |
| 6040 | if (kid) |
| 6041 | return too_many_arguments(o,OP_DESC(o)); |
| 6042 | listkids(o); |
| 6043 | } |
| 6044 | else if (PL_opargs[type] & OA_DEFGV) { |
| 6045 | op_free(o); |
| 6046 | return newUNOP(type, 0, newDEFSVOP()); |
| 6047 | } |
| 6048 | |
| 6049 | if (oa) { |
| 6050 | while (oa & OA_OPTIONAL) |
| 6051 | oa >>= 4; |
| 6052 | if (oa && oa != OA_LIST) |
| 6053 | return too_few_arguments(o,OP_DESC(o)); |
| 6054 | } |
| 6055 | return o; |
| 6056 | } |
| 6057 | |
| 6058 | OP * |
| 6059 | Perl_ck_glob(pTHX_ OP *o) |
| 6060 | { |
| 6061 | GV *gv; |
| 6062 | |
| 6063 | o = ck_fun(o); |
| 6064 | if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling) |
| 6065 | append_elem(OP_GLOB, o, newDEFSVOP()); |
| 6066 | |
| 6067 | if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) |
| 6068 | && GvCVu(gv) && GvIMPORTED_CV(gv))) |
| 6069 | { |
| 6070 | gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV); |
| 6071 | } |
| 6072 | |
| 6073 | #if !defined(PERL_EXTERNAL_GLOB) |
| 6074 | /* XXX this can be tightened up and made more failsafe. */ |
| 6075 | if (!gv) { |
| 6076 | GV *glob_gv; |
| 6077 | ENTER; |
| 6078 | Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, |
| 6079 | newSVpvn("File::Glob", 10), Nullsv, Nullsv, Nullsv); |
| 6080 | gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV); |
| 6081 | glob_gv = gv_fetchpv("File::Glob::csh_glob", FALSE, SVt_PVCV); |
| 6082 | GvCV(gv) = GvCV(glob_gv); |
| 6083 | SvREFCNT_inc((SV*)GvCV(gv)); |
| 6084 | GvIMPORTED_CV_on(gv); |
| 6085 | LEAVE; |
| 6086 | } |
| 6087 | #endif /* PERL_EXTERNAL_GLOB */ |
| 6088 | |
| 6089 | if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) { |
| 6090 | append_elem(OP_GLOB, o, |
| 6091 | newSVOP(OP_CONST, 0, newSViv(PL_glob_index++))); |
| 6092 | o->op_type = OP_LIST; |
| 6093 | o->op_ppaddr = PL_ppaddr[OP_LIST]; |
| 6094 | cLISTOPo->op_first->op_type = OP_PUSHMARK; |
| 6095 | cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK]; |
| 6096 | o = newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 6097 | append_elem(OP_LIST, o, |
| 6098 | scalar(newUNOP(OP_RV2CV, 0, |
| 6099 | newGVOP(OP_GV, 0, gv))))); |
| 6100 | o = newUNOP(OP_NULL, 0, ck_subr(o)); |
| 6101 | o->op_targ = OP_GLOB; /* hint at what it used to be */ |
| 6102 | return o; |
| 6103 | } |
| 6104 | gv = newGVgen("main"); |
| 6105 | gv_IOadd(gv); |
| 6106 | append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); |
| 6107 | scalarkids(o); |
| 6108 | return o; |
| 6109 | } |
| 6110 | |
| 6111 | OP * |
| 6112 | Perl_ck_grep(pTHX_ OP *o) |
| 6113 | { |
| 6114 | LOGOP *gwop; |
| 6115 | OP *kid; |
| 6116 | OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; |
| 6117 | |
| 6118 | o->op_ppaddr = PL_ppaddr[OP_GREPSTART]; |
| 6119 | NewOp(1101, gwop, 1, LOGOP); |
| 6120 | |
| 6121 | if (o->op_flags & OPf_STACKED) { |
| 6122 | OP* k; |
| 6123 | o = ck_sort(o); |
| 6124 | kid = cLISTOPo->op_first->op_sibling; |
| 6125 | for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) { |
| 6126 | kid = k; |
| 6127 | } |
| 6128 | kid->op_next = (OP*)gwop; |
| 6129 | o->op_flags &= ~OPf_STACKED; |
| 6130 | } |
| 6131 | kid = cLISTOPo->op_first->op_sibling; |
| 6132 | if (type == OP_MAPWHILE) |
| 6133 | list(kid); |
| 6134 | else |
| 6135 | scalar(kid); |
| 6136 | o = ck_fun(o); |
| 6137 | if (PL_error_count) |
| 6138 | return o; |
| 6139 | kid = cLISTOPo->op_first->op_sibling; |
| 6140 | if (kid->op_type != OP_NULL) |
| 6141 | Perl_croak(aTHX_ "panic: ck_grep"); |
| 6142 | kid = kUNOP->op_first; |
| 6143 | |
| 6144 | gwop->op_type = type; |
| 6145 | gwop->op_ppaddr = PL_ppaddr[type]; |
| 6146 | gwop->op_first = listkids(o); |
| 6147 | gwop->op_flags |= OPf_KIDS; |
| 6148 | gwop->op_private = 1; |
| 6149 | gwop->op_other = LINKLIST(kid); |
| 6150 | gwop->op_targ = pad_alloc(type, SVs_PADTMP); |
| 6151 | kid->op_next = (OP*)gwop; |
| 6152 | |
| 6153 | kid = cLISTOPo->op_first->op_sibling; |
| 6154 | if (!kid || !kid->op_sibling) |
| 6155 | return too_few_arguments(o,OP_DESC(o)); |
| 6156 | for (kid = kid->op_sibling; kid; kid = kid->op_sibling) |
| 6157 | mod(kid, OP_GREPSTART); |
| 6158 | |
| 6159 | return (OP*)gwop; |
| 6160 | } |
| 6161 | |
| 6162 | OP * |
| 6163 | Perl_ck_index(pTHX_ OP *o) |
| 6164 | { |
| 6165 | if (o->op_flags & OPf_KIDS) { |
| 6166 | OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
| 6167 | if (kid) |
| 6168 | kid = kid->op_sibling; /* get past "big" */ |
| 6169 | if (kid && kid->op_type == OP_CONST) |
| 6170 | fbm_compile(((SVOP*)kid)->op_sv, 0); |
| 6171 | } |
| 6172 | return ck_fun(o); |
| 6173 | } |
| 6174 | |
| 6175 | OP * |
| 6176 | Perl_ck_lengthconst(pTHX_ OP *o) |
| 6177 | { |
| 6178 | /* XXX length optimization goes here */ |
| 6179 | return ck_fun(o); |
| 6180 | } |
| 6181 | |
| 6182 | OP * |
| 6183 | Perl_ck_lfun(pTHX_ OP *o) |
| 6184 | { |
| 6185 | OPCODE type = o->op_type; |
| 6186 | return modkids(ck_fun(o), type); |
| 6187 | } |
| 6188 | |
| 6189 | OP * |
| 6190 | Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */ |
| 6191 | { |
| 6192 | if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) { |
| 6193 | switch (cUNOPo->op_first->op_type) { |
| 6194 | case OP_RV2AV: |
| 6195 | /* This is needed for |
| 6196 | if (defined %stash::) |
| 6197 | to work. Do not break Tk. |
| 6198 | */ |
| 6199 | break; /* Globals via GV can be undef */ |
| 6200 | case OP_PADAV: |
| 6201 | case OP_AASSIGN: /* Is this a good idea? */ |
| 6202 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 6203 | "defined(@array) is deprecated"); |
| 6204 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 6205 | "\t(Maybe you should just omit the defined()?)\n"); |
| 6206 | break; |
| 6207 | case OP_RV2HV: |
| 6208 | /* This is needed for |
| 6209 | if (defined %stash::) |
| 6210 | to work. Do not break Tk. |
| 6211 | */ |
| 6212 | break; /* Globals via GV can be undef */ |
| 6213 | case OP_PADHV: |
| 6214 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 6215 | "defined(%%hash) is deprecated"); |
| 6216 | Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX), |
| 6217 | "\t(Maybe you should just omit the defined()?)\n"); |
| 6218 | break; |
| 6219 | default: |
| 6220 | /* no warning */ |
| 6221 | break; |
| 6222 | } |
| 6223 | } |
| 6224 | return ck_rfun(o); |
| 6225 | } |
| 6226 | |
| 6227 | OP * |
| 6228 | Perl_ck_rfun(pTHX_ OP *o) |
| 6229 | { |
| 6230 | OPCODE type = o->op_type; |
| 6231 | return refkids(ck_fun(o), type); |
| 6232 | } |
| 6233 | |
| 6234 | OP * |
| 6235 | Perl_ck_listiob(pTHX_ OP *o) |
| 6236 | { |
| 6237 | register OP *kid; |
| 6238 | |
| 6239 | kid = cLISTOPo->op_first; |
| 6240 | if (!kid) { |
| 6241 | o = force_list(o); |
| 6242 | kid = cLISTOPo->op_first; |
| 6243 | } |
| 6244 | if (kid->op_type == OP_PUSHMARK) |
| 6245 | kid = kid->op_sibling; |
| 6246 | if (kid && o->op_flags & OPf_STACKED) |
| 6247 | kid = kid->op_sibling; |
| 6248 | else if (kid && !kid->op_sibling) { /* print HANDLE; */ |
| 6249 | if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) { |
| 6250 | o->op_flags |= OPf_STACKED; /* make it a filehandle */ |
| 6251 | kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid)); |
| 6252 | cLISTOPo->op_first->op_sibling = kid; |
| 6253 | cLISTOPo->op_last = kid; |
| 6254 | kid = kid->op_sibling; |
| 6255 | } |
| 6256 | } |
| 6257 | |
| 6258 | if (!kid) |
| 6259 | append_elem(o->op_type, o, newDEFSVOP()); |
| 6260 | |
| 6261 | return listkids(o); |
| 6262 | } |
| 6263 | |
| 6264 | OP * |
| 6265 | Perl_ck_sassign(pTHX_ OP *o) |
| 6266 | { |
| 6267 | OP *kid = cLISTOPo->op_first; |
| 6268 | /* has a disposable target? */ |
| 6269 | if ((PL_opargs[kid->op_type] & OA_TARGLEX) |
| 6270 | && !(kid->op_flags & OPf_STACKED) |
| 6271 | /* Cannot steal the second time! */ |
| 6272 | && !(kid->op_private & OPpTARGET_MY)) |
| 6273 | { |
| 6274 | OP *kkid = kid->op_sibling; |
| 6275 | |
| 6276 | /* Can just relocate the target. */ |
| 6277 | if (kkid && kkid->op_type == OP_PADSV |
| 6278 | && !(kkid->op_private & OPpLVAL_INTRO)) |
| 6279 | { |
| 6280 | kid->op_targ = kkid->op_targ; |
| 6281 | kkid->op_targ = 0; |
| 6282 | /* Now we do not need PADSV and SASSIGN. */ |
| 6283 | kid->op_sibling = o->op_sibling; /* NULL */ |
| 6284 | cLISTOPo->op_first = NULL; |
| 6285 | op_free(o); |
| 6286 | op_free(kkid); |
| 6287 | kid->op_private |= OPpTARGET_MY; /* Used for context settings */ |
| 6288 | return kid; |
| 6289 | } |
| 6290 | } |
| 6291 | return o; |
| 6292 | } |
| 6293 | |
| 6294 | OP * |
| 6295 | Perl_ck_match(pTHX_ OP *o) |
| 6296 | { |
| 6297 | o->op_private |= OPpRUNTIME; |
| 6298 | return o; |
| 6299 | } |
| 6300 | |
| 6301 | OP * |
| 6302 | Perl_ck_method(pTHX_ OP *o) |
| 6303 | { |
| 6304 | OP *kid = cUNOPo->op_first; |
| 6305 | if (kid->op_type == OP_CONST) { |
| 6306 | SV* sv = kSVOP->op_sv; |
| 6307 | if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) { |
| 6308 | OP *cmop; |
| 6309 | if (!SvREADONLY(sv) || !SvFAKE(sv)) { |
| 6310 | sv = newSVpvn_share(SvPVX(sv), SvCUR(sv), 0); |
| 6311 | } |
| 6312 | else { |
| 6313 | kSVOP->op_sv = Nullsv; |
| 6314 | } |
| 6315 | cmop = newSVOP(OP_METHOD_NAMED, 0, sv); |
| 6316 | op_free(o); |
| 6317 | return cmop; |
| 6318 | } |
| 6319 | } |
| 6320 | return o; |
| 6321 | } |
| 6322 | |
| 6323 | OP * |
| 6324 | Perl_ck_null(pTHX_ OP *o) |
| 6325 | { |
| 6326 | return o; |
| 6327 | } |
| 6328 | |
| 6329 | OP * |
| 6330 | Perl_ck_open(pTHX_ OP *o) |
| 6331 | { |
| 6332 | HV *table = GvHV(PL_hintgv); |
| 6333 | if (table) { |
| 6334 | SV **svp; |
| 6335 | I32 mode; |
| 6336 | svp = hv_fetch(table, "open_IN", 7, FALSE); |
| 6337 | if (svp && *svp) { |
| 6338 | mode = mode_from_discipline(*svp); |
| 6339 | if (mode & O_BINARY) |
| 6340 | o->op_private |= OPpOPEN_IN_RAW; |
| 6341 | else if (mode & O_TEXT) |
| 6342 | o->op_private |= OPpOPEN_IN_CRLF; |
| 6343 | } |
| 6344 | |
| 6345 | svp = hv_fetch(table, "open_OUT", 8, FALSE); |
| 6346 | if (svp && *svp) { |
| 6347 | mode = mode_from_discipline(*svp); |
| 6348 | if (mode & O_BINARY) |
| 6349 | o->op_private |= OPpOPEN_OUT_RAW; |
| 6350 | else if (mode & O_TEXT) |
| 6351 | o->op_private |= OPpOPEN_OUT_CRLF; |
| 6352 | } |
| 6353 | } |
| 6354 | if (o->op_type == OP_BACKTICK) |
| 6355 | return o; |
| 6356 | return ck_fun(o); |
| 6357 | } |
| 6358 | |
| 6359 | OP * |
| 6360 | Perl_ck_repeat(pTHX_ OP *o) |
| 6361 | { |
| 6362 | if (cBINOPo->op_first->op_flags & OPf_PARENS) { |
| 6363 | o->op_private |= OPpREPEAT_DOLIST; |
| 6364 | cBINOPo->op_first = force_list(cBINOPo->op_first); |
| 6365 | } |
| 6366 | else |
| 6367 | scalar(o); |
| 6368 | return o; |
| 6369 | } |
| 6370 | |
| 6371 | OP * |
| 6372 | Perl_ck_require(pTHX_ OP *o) |
| 6373 | { |
| 6374 | GV* gv; |
| 6375 | |
| 6376 | if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ |
| 6377 | SVOP *kid = (SVOP*)cUNOPo->op_first; |
| 6378 | |
| 6379 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { |
| 6380 | char *s; |
| 6381 | for (s = SvPVX(kid->op_sv); *s; s++) { |
| 6382 | if (*s == ':' && s[1] == ':') { |
| 6383 | *s = '/'; |
| 6384 | Move(s+2, s+1, strlen(s+2)+1, char); |
| 6385 | --SvCUR(kid->op_sv); |
| 6386 | } |
| 6387 | } |
| 6388 | if (SvREADONLY(kid->op_sv)) { |
| 6389 | SvREADONLY_off(kid->op_sv); |
| 6390 | sv_catpvn(kid->op_sv, ".pm", 3); |
| 6391 | SvREADONLY_on(kid->op_sv); |
| 6392 | } |
| 6393 | else |
| 6394 | sv_catpvn(kid->op_sv, ".pm", 3); |
| 6395 | } |
| 6396 | } |
| 6397 | |
| 6398 | /* handle override, if any */ |
| 6399 | gv = gv_fetchpv("require", FALSE, SVt_PVCV); |
| 6400 | if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) |
| 6401 | gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV); |
| 6402 | |
| 6403 | if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) { |
| 6404 | OP *kid = cUNOPo->op_first; |
| 6405 | cUNOPo->op_first = 0; |
| 6406 | op_free(o); |
| 6407 | return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED, |
| 6408 | append_elem(OP_LIST, kid, |
| 6409 | scalar(newUNOP(OP_RV2CV, 0, |
| 6410 | newGVOP(OP_GV, 0, |
| 6411 | gv)))))); |
| 6412 | } |
| 6413 | |
| 6414 | return ck_fun(o); |
| 6415 | } |
| 6416 | |
| 6417 | OP * |
| 6418 | Perl_ck_return(pTHX_ OP *o) |
| 6419 | { |
| 6420 | OP *kid; |
| 6421 | if (CvLVALUE(PL_compcv)) { |
| 6422 | for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
| 6423 | mod(kid, OP_LEAVESUBLV); |
| 6424 | } |
| 6425 | return o; |
| 6426 | } |
| 6427 | |
| 6428 | #if 0 |
| 6429 | OP * |
| 6430 | Perl_ck_retarget(pTHX_ OP *o) |
| 6431 | { |
| 6432 | Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__); |
| 6433 | /* STUB */ |
| 6434 | return o; |
| 6435 | } |
| 6436 | #endif |
| 6437 | |
| 6438 | OP * |
| 6439 | Perl_ck_select(pTHX_ OP *o) |
| 6440 | { |
| 6441 | OP* kid; |
| 6442 | if (o->op_flags & OPf_KIDS) { |
| 6443 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
| 6444 | if (kid && kid->op_sibling) { |
| 6445 | o->op_type = OP_SSELECT; |
| 6446 | o->op_ppaddr = PL_ppaddr[OP_SSELECT]; |
| 6447 | o = ck_fun(o); |
| 6448 | return fold_constants(o); |
| 6449 | } |
| 6450 | } |
| 6451 | o = ck_fun(o); |
| 6452 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
| 6453 | if (kid && kid->op_type == OP_RV2GV) |
| 6454 | kid->op_private &= ~HINT_STRICT_REFS; |
| 6455 | return o; |
| 6456 | } |
| 6457 | |
| 6458 | OP * |
| 6459 | Perl_ck_shift(pTHX_ OP *o) |
| 6460 | { |
| 6461 | I32 type = o->op_type; |
| 6462 | |
| 6463 | if (!(o->op_flags & OPf_KIDS)) { |
| 6464 | OP *argop; |
| 6465 | |
| 6466 | op_free(o); |
| 6467 | #ifdef USE_5005THREADS |
| 6468 | if (!CvUNIQUE(PL_compcv)) { |
| 6469 | argop = newOP(OP_PADAV, OPf_REF); |
| 6470 | argop->op_targ = 0; /* PL_curpad[0] is @_ */ |
| 6471 | } |
| 6472 | else { |
| 6473 | argop = newUNOP(OP_RV2AV, 0, |
| 6474 | scalar(newGVOP(OP_GV, 0, |
| 6475 | gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); |
| 6476 | } |
| 6477 | #else |
| 6478 | argop = newUNOP(OP_RV2AV, 0, |
| 6479 | scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ? |
| 6480 | PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); |
| 6481 | #endif /* USE_5005THREADS */ |
| 6482 | return newUNOP(type, 0, scalar(argop)); |
| 6483 | } |
| 6484 | return scalar(modkids(ck_fun(o), type)); |
| 6485 | } |
| 6486 | |
| 6487 | OP * |
| 6488 | Perl_ck_sort(pTHX_ OP *o) |
| 6489 | { |
| 6490 | OP *firstkid; |
| 6491 | |
| 6492 | if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED) |
| 6493 | simplify_sort(o); |
| 6494 | firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
| 6495 | if (o->op_flags & OPf_STACKED) { /* may have been cleared */ |
| 6496 | OP *k = NULL; |
| 6497 | OP *kid = cUNOPx(firstkid)->op_first; /* get past null */ |
| 6498 | |
| 6499 | if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { |
| 6500 | linklist(kid); |
| 6501 | if (kid->op_type == OP_SCOPE) { |
| 6502 | k = kid->op_next; |
| 6503 | kid->op_next = 0; |
| 6504 | } |
| 6505 | else if (kid->op_type == OP_LEAVE) { |
| 6506 | if (o->op_type == OP_SORT) { |
| 6507 | op_null(kid); /* wipe out leave */ |
| 6508 | kid->op_next = kid; |
| 6509 | |
| 6510 | for (k = kLISTOP->op_first->op_next; k; k = k->op_next) { |
| 6511 | if (k->op_next == kid) |
| 6512 | k->op_next = 0; |
| 6513 | /* don't descend into loops */ |
| 6514 | else if (k->op_type == OP_ENTERLOOP |
| 6515 | || k->op_type == OP_ENTERITER) |
| 6516 | { |
| 6517 | k = cLOOPx(k)->op_lastop; |
| 6518 | } |
| 6519 | } |
| 6520 | } |
| 6521 | else |
| 6522 | kid->op_next = 0; /* just disconnect the leave */ |
| 6523 | k = kLISTOP->op_first; |
| 6524 | } |
| 6525 | CALL_PEEP(k); |
| 6526 | |
| 6527 | kid = firstkid; |
| 6528 | if (o->op_type == OP_SORT) { |
| 6529 | /* provide scalar context for comparison function/block */ |
| 6530 | kid = scalar(kid); |
| 6531 | kid->op_next = kid; |
| 6532 | } |
| 6533 | else |
| 6534 | kid->op_next = k; |
| 6535 | o->op_flags |= OPf_SPECIAL; |
| 6536 | } |
| 6537 | else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV) |
| 6538 | op_null(firstkid); |
| 6539 | |
| 6540 | firstkid = firstkid->op_sibling; |
| 6541 | } |
| 6542 | |
| 6543 | /* provide list context for arguments */ |
| 6544 | if (o->op_type == OP_SORT) |
| 6545 | list(firstkid); |
| 6546 | |
| 6547 | return o; |
| 6548 | } |
| 6549 | |
| 6550 | STATIC void |
| 6551 | S_simplify_sort(pTHX_ OP *o) |
| 6552 | { |
| 6553 | register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
| 6554 | OP *k; |
| 6555 | int reversed; |
| 6556 | GV *gv; |
| 6557 | if (!(o->op_flags & OPf_STACKED)) |
| 6558 | return; |
| 6559 | GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV)); |
| 6560 | GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV)); |
| 6561 | kid = kUNOP->op_first; /* get past null */ |
| 6562 | if (kid->op_type != OP_SCOPE) |
| 6563 | return; |
| 6564 | kid = kLISTOP->op_last; /* get past scope */ |
| 6565 | switch(kid->op_type) { |
| 6566 | case OP_NCMP: |
| 6567 | case OP_I_NCMP: |
| 6568 | case OP_SCMP: |
| 6569 | break; |
| 6570 | default: |
| 6571 | return; |
| 6572 | } |
| 6573 | k = kid; /* remember this node*/ |
| 6574 | if (kBINOP->op_first->op_type != OP_RV2SV) |
| 6575 | return; |
| 6576 | kid = kBINOP->op_first; /* get past cmp */ |
| 6577 | if (kUNOP->op_first->op_type != OP_GV) |
| 6578 | return; |
| 6579 | kid = kUNOP->op_first; /* get past rv2sv */ |
| 6580 | gv = kGVOP_gv; |
| 6581 | if (GvSTASH(gv) != PL_curstash) |
| 6582 | return; |
| 6583 | if (strEQ(GvNAME(gv), "a")) |
| 6584 | reversed = 0; |
| 6585 | else if (strEQ(GvNAME(gv), "b")) |
| 6586 | reversed = 1; |
| 6587 | else |
| 6588 | return; |
| 6589 | kid = k; /* back to cmp */ |
| 6590 | if (kBINOP->op_last->op_type != OP_RV2SV) |
| 6591 | return; |
| 6592 | kid = kBINOP->op_last; /* down to 2nd arg */ |
| 6593 | if (kUNOP->op_first->op_type != OP_GV) |
| 6594 | return; |
| 6595 | kid = kUNOP->op_first; /* get past rv2sv */ |
| 6596 | gv = kGVOP_gv; |
| 6597 | if (GvSTASH(gv) != PL_curstash |
| 6598 | || ( reversed |
| 6599 | ? strNE(GvNAME(gv), "a") |
| 6600 | : strNE(GvNAME(gv), "b"))) |
| 6601 | return; |
| 6602 | o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL); |
| 6603 | if (reversed) |
| 6604 | o->op_private |= OPpSORT_REVERSE; |
| 6605 | if (k->op_type == OP_NCMP) |
| 6606 | o->op_private |= OPpSORT_NUMERIC; |
| 6607 | if (k->op_type == OP_I_NCMP) |
| 6608 | o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER; |
| 6609 | kid = cLISTOPo->op_first->op_sibling; |
| 6610 | cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */ |
| 6611 | op_free(kid); /* then delete it */ |
| 6612 | } |
| 6613 | |
| 6614 | OP * |
| 6615 | Perl_ck_split(pTHX_ OP *o) |
| 6616 | { |
| 6617 | register OP *kid; |
| 6618 | |
| 6619 | if (o->op_flags & OPf_STACKED) |
| 6620 | return no_fh_allowed(o); |
| 6621 | |
| 6622 | kid = cLISTOPo->op_first; |
| 6623 | if (kid->op_type != OP_NULL) |
| 6624 | Perl_croak(aTHX_ "panic: ck_split"); |
| 6625 | kid = kid->op_sibling; |
| 6626 | op_free(cLISTOPo->op_first); |
| 6627 | cLISTOPo->op_first = kid; |
| 6628 | if (!kid) { |
| 6629 | cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1)); |
| 6630 | cLISTOPo->op_last = kid; /* There was only one element previously */ |
| 6631 | } |
| 6632 | |
| 6633 | if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) { |
| 6634 | OP *sibl = kid->op_sibling; |
| 6635 | kid->op_sibling = 0; |
| 6636 | kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop); |
| 6637 | if (cLISTOPo->op_first == cLISTOPo->op_last) |
| 6638 | cLISTOPo->op_last = kid; |
| 6639 | cLISTOPo->op_first = kid; |
| 6640 | kid->op_sibling = sibl; |
| 6641 | } |
| 6642 | |
| 6643 | kid->op_type = OP_PUSHRE; |
| 6644 | kid->op_ppaddr = PL_ppaddr[OP_PUSHRE]; |
| 6645 | scalar(kid); |
| 6646 | if (ckWARN(WARN_REGEXP) && ((PMOP *)kid)->op_pmflags & PMf_GLOBAL) { |
| 6647 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
| 6648 | "Use of /g modifier is meaningless in split"); |
| 6649 | } |
| 6650 | |
| 6651 | if (!kid->op_sibling) |
| 6652 | append_elem(OP_SPLIT, o, newDEFSVOP()); |
| 6653 | |
| 6654 | kid = kid->op_sibling; |
| 6655 | scalar(kid); |
| 6656 | |
| 6657 | if (!kid->op_sibling) |
| 6658 | append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0))); |
| 6659 | |
| 6660 | kid = kid->op_sibling; |
| 6661 | scalar(kid); |
| 6662 | |
| 6663 | if (kid->op_sibling) |
| 6664 | return too_many_arguments(o,OP_DESC(o)); |
| 6665 | |
| 6666 | return o; |
| 6667 | } |
| 6668 | |
| 6669 | OP * |
| 6670 | Perl_ck_join(pTHX_ OP *o) |
| 6671 | { |
| 6672 | if (ckWARN(WARN_SYNTAX)) { |
| 6673 | OP *kid = cLISTOPo->op_first->op_sibling; |
| 6674 | if (kid && kid->op_type == OP_MATCH) { |
| 6675 | char *pmstr = "STRING"; |
| 6676 | if (PM_GETRE(kPMOP)) |
| 6677 | pmstr = PM_GETRE(kPMOP)->precomp; |
| 6678 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), |
| 6679 | "/%s/ should probably be written as \"%s\"", |
| 6680 | pmstr, pmstr); |
| 6681 | } |
| 6682 | } |
| 6683 | return ck_fun(o); |
| 6684 | } |
| 6685 | |
| 6686 | OP * |
| 6687 | Perl_ck_subr(pTHX_ OP *o) |
| 6688 | { |
| 6689 | OP *prev = ((cUNOPo->op_first->op_sibling) |
| 6690 | ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first; |
| 6691 | OP *o2 = prev->op_sibling; |
| 6692 | OP *cvop; |
| 6693 | char *proto = 0; |
| 6694 | CV *cv = 0; |
| 6695 | GV *namegv = 0; |
| 6696 | int optional = 0; |
| 6697 | I32 arg = 0; |
| 6698 | I32 contextclass = 0; |
| 6699 | char *e = 0; |
| 6700 | STRLEN n_a; |
| 6701 | |
| 6702 | o->op_private |= OPpENTERSUB_HASTARG; |
| 6703 | for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ; |
| 6704 | if (cvop->op_type == OP_RV2CV) { |
| 6705 | SVOP* tmpop; |
| 6706 | o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); |
| 6707 | op_null(cvop); /* disable rv2cv */ |
| 6708 | tmpop = (SVOP*)((UNOP*)cvop)->op_first; |
| 6709 | if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) { |
| 6710 | GV *gv = cGVOPx_gv(tmpop); |
| 6711 | cv = GvCVu(gv); |
| 6712 | if (!cv) |
| 6713 | tmpop->op_private |= OPpEARLY_CV; |
| 6714 | else if (SvPOK(cv)) { |
| 6715 | namegv = CvANON(cv) ? gv : CvGV(cv); |
| 6716 | proto = SvPV((SV*)cv, n_a); |
| 6717 | } |
| 6718 | } |
| 6719 | } |
| 6720 | else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) { |
| 6721 | if (o2->op_type == OP_CONST) |
| 6722 | o2->op_private &= ~OPpCONST_STRICT; |
| 6723 | else if (o2->op_type == OP_LIST) { |
| 6724 | OP *o = ((UNOP*)o2)->op_first->op_sibling; |
| 6725 | if (o && o->op_type == OP_CONST) |
| 6726 | o->op_private &= ~OPpCONST_STRICT; |
| 6727 | } |
| 6728 | } |
| 6729 | o->op_private |= (PL_hints & HINT_STRICT_REFS); |
| 6730 | if (PERLDB_SUB && PL_curstash != PL_debstash) |
| 6731 | o->op_private |= OPpENTERSUB_DB; |
| 6732 | while (o2 != cvop) { |
| 6733 | if (proto) { |
| 6734 | switch (*proto) { |
| 6735 | case '\0': |
| 6736 | return too_many_arguments(o, gv_ename(namegv)); |
| 6737 | case ';': |
| 6738 | optional = 1; |
| 6739 | proto++; |
| 6740 | continue; |
| 6741 | case '$': |
| 6742 | proto++; |
| 6743 | arg++; |
| 6744 | scalar(o2); |
| 6745 | break; |
| 6746 | case '%': |
| 6747 | case '@': |
| 6748 | list(o2); |
| 6749 | arg++; |
| 6750 | break; |
| 6751 | case '&': |
| 6752 | proto++; |
| 6753 | arg++; |
| 6754 | if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF) |
| 6755 | bad_type(arg, |
| 6756 | arg == 1 ? "block or sub {}" : "sub {}", |
| 6757 | gv_ename(namegv), o2); |
| 6758 | break; |
| 6759 | case '*': |
| 6760 | /* '*' allows any scalar type, including bareword */ |
| 6761 | proto++; |
| 6762 | arg++; |
| 6763 | if (o2->op_type == OP_RV2GV) |
| 6764 | goto wrapref; /* autoconvert GLOB -> GLOBref */ |
| 6765 | else if (o2->op_type == OP_CONST) |
| 6766 | o2->op_private &= ~OPpCONST_STRICT; |
| 6767 | else if (o2->op_type == OP_ENTERSUB) { |
| 6768 | /* accidental subroutine, revert to bareword */ |
| 6769 | OP *gvop = ((UNOP*)o2)->op_first; |
| 6770 | if (gvop && gvop->op_type == OP_NULL) { |
| 6771 | gvop = ((UNOP*)gvop)->op_first; |
| 6772 | if (gvop) { |
| 6773 | for (; gvop->op_sibling; gvop = gvop->op_sibling) |
| 6774 | ; |
| 6775 | if (gvop && |
| 6776 | (gvop->op_private & OPpENTERSUB_NOPAREN) && |
| 6777 | (gvop = ((UNOP*)gvop)->op_first) && |
| 6778 | gvop->op_type == OP_GV) |
| 6779 | { |
| 6780 | GV *gv = cGVOPx_gv(gvop); |
| 6781 | OP *sibling = o2->op_sibling; |
| 6782 | SV *n = newSVpvn("",0); |
| 6783 | op_free(o2); |
| 6784 | gv_fullname3(n, gv, ""); |
| 6785 | if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6)) |
| 6786 | sv_chop(n, SvPVX(n)+6); |
| 6787 | o2 = newSVOP(OP_CONST, 0, n); |
| 6788 | prev->op_sibling = o2; |
| 6789 | o2->op_sibling = sibling; |
| 6790 | } |
| 6791 | } |
| 6792 | } |
| 6793 | } |
| 6794 | scalar(o2); |
| 6795 | break; |
| 6796 | case '[': case ']': |
| 6797 | goto oops; |
| 6798 | break; |
| 6799 | case '\\': |
| 6800 | proto++; |
| 6801 | arg++; |
| 6802 | again: |
| 6803 | switch (*proto++) { |
| 6804 | case '[': |
| 6805 | if (contextclass++ == 0) { |
| 6806 | e = strchr(proto, ']'); |
| 6807 | if (!e || e == proto) |
| 6808 | goto oops; |
| 6809 | } |
| 6810 | else |
| 6811 | goto oops; |
| 6812 | goto again; |
| 6813 | break; |
| 6814 | case ']': |
| 6815 | if (contextclass) { |
| 6816 | char *p = proto; |
| 6817 | char s = *p; |
| 6818 | contextclass = 0; |
| 6819 | *p = '\0'; |
| 6820 | while (*--p != '['); |
| 6821 | bad_type(arg, Perl_form(aTHX_ "one of %s", p), |
| 6822 | gv_ename(namegv), o2); |
| 6823 | *proto = s; |
| 6824 | } else |
| 6825 | goto oops; |
| 6826 | break; |
| 6827 | case '*': |
| 6828 | if (o2->op_type == OP_RV2GV) |
| 6829 | goto wrapref; |
| 6830 | if (!contextclass) |
| 6831 | bad_type(arg, "symbol", gv_ename(namegv), o2); |
| 6832 | break; |
| 6833 | case '&': |
| 6834 | if (o2->op_type == OP_ENTERSUB) |
| 6835 | goto wrapref; |
| 6836 | if (!contextclass) |
| 6837 | bad_type(arg, "subroutine entry", gv_ename(namegv), o2); |
| 6838 | break; |
| 6839 | case '$': |
| 6840 | if (o2->op_type == OP_RV2SV || |
| 6841 | o2->op_type == OP_PADSV || |
| 6842 | o2->op_type == OP_HELEM || |
| 6843 | o2->op_type == OP_AELEM || |
| 6844 | o2->op_type == OP_THREADSV) |
| 6845 | goto wrapref; |
| 6846 | if (!contextclass) |
| 6847 | bad_type(arg, "scalar", gv_ename(namegv), o2); |
| 6848 | break; |
| 6849 | case '@': |
| 6850 | if (o2->op_type == OP_RV2AV || |
| 6851 | o2->op_type == OP_PADAV) |
| 6852 | goto wrapref; |
| 6853 | if (!contextclass) |
| 6854 | bad_type(arg, "array", gv_ename(namegv), o2); |
| 6855 | break; |
| 6856 | case '%': |
| 6857 | if (o2->op_type == OP_RV2HV || |
| 6858 | o2->op_type == OP_PADHV) |
| 6859 | goto wrapref; |
| 6860 | if (!contextclass) |
| 6861 | bad_type(arg, "hash", gv_ename(namegv), o2); |
| 6862 | break; |
| 6863 | wrapref: |
| 6864 | { |
| 6865 | OP* kid = o2; |
| 6866 | OP* sib = kid->op_sibling; |
| 6867 | kid->op_sibling = 0; |
| 6868 | o2 = newUNOP(OP_REFGEN, 0, kid); |
| 6869 | o2->op_sibling = sib; |
| 6870 | prev->op_sibling = o2; |
| 6871 | } |
| 6872 | if (contextclass && e) { |
| 6873 | proto = e + 1; |
| 6874 | contextclass = 0; |
| 6875 | } |
| 6876 | break; |
| 6877 | default: goto oops; |
| 6878 | } |
| 6879 | if (contextclass) |
| 6880 | goto again; |
| 6881 | break; |
| 6882 | case ' ': |
| 6883 | proto++; |
| 6884 | continue; |
| 6885 | default: |
| 6886 | oops: |
| 6887 | Perl_croak(aTHX_ "Malformed prototype for %s: %s", |
| 6888 | gv_ename(namegv), SvPV((SV*)cv, n_a)); |
| 6889 | } |
| 6890 | } |
| 6891 | else |
| 6892 | list(o2); |
| 6893 | mod(o2, OP_ENTERSUB); |
| 6894 | prev = o2; |
| 6895 | o2 = o2->op_sibling; |
| 6896 | } |
| 6897 | if (proto && !optional && |
| 6898 | (*proto && *proto != '@' && *proto != '%' && *proto != ';')) |
| 6899 | return too_few_arguments(o, gv_ename(namegv)); |
| 6900 | return o; |
| 6901 | } |
| 6902 | |
| 6903 | OP * |
| 6904 | Perl_ck_svconst(pTHX_ OP *o) |
| 6905 | { |
| 6906 | SvREADONLY_on(cSVOPo->op_sv); |
| 6907 | return o; |
| 6908 | } |
| 6909 | |
| 6910 | OP * |
| 6911 | Perl_ck_trunc(pTHX_ OP *o) |
| 6912 | { |
| 6913 | if (o->op_flags & OPf_KIDS) { |
| 6914 | SVOP *kid = (SVOP*)cUNOPo->op_first; |
| 6915 | |
| 6916 | if (kid->op_type == OP_NULL) |
| 6917 | kid = (SVOP*)kid->op_sibling; |
| 6918 | if (kid && kid->op_type == OP_CONST && |
| 6919 | (kid->op_private & OPpCONST_BARE)) |
| 6920 | { |
| 6921 | o->op_flags |= OPf_SPECIAL; |
| 6922 | kid->op_private &= ~OPpCONST_STRICT; |
| 6923 | } |
| 6924 | } |
| 6925 | return ck_fun(o); |
| 6926 | } |
| 6927 | |
| 6928 | OP * |
| 6929 | Perl_ck_substr(pTHX_ OP *o) |
| 6930 | { |
| 6931 | o = ck_fun(o); |
| 6932 | if ((o->op_flags & OPf_KIDS) && o->op_private == 4) { |
| 6933 | OP *kid = cLISTOPo->op_first; |
| 6934 | |
| 6935 | if (kid->op_type == OP_NULL) |
| 6936 | kid = kid->op_sibling; |
| 6937 | if (kid) |
| 6938 | kid->op_flags |= OPf_MOD; |
| 6939 | |
| 6940 | } |
| 6941 | return o; |
| 6942 | } |
| 6943 | |
| 6944 | /* A peephole optimizer. We visit the ops in the order they're to execute. */ |
| 6945 | |
| 6946 | void |
| 6947 | Perl_peep(pTHX_ register OP *o) |
| 6948 | { |
| 6949 | register OP* oldop = 0; |
| 6950 | STRLEN n_a; |
| 6951 | |
| 6952 | if (!o || o->op_seq) |
| 6953 | return; |
| 6954 | ENTER; |
| 6955 | SAVEOP(); |
| 6956 | SAVEVPTR(PL_curcop); |
| 6957 | for (; o; o = o->op_next) { |
| 6958 | if (o->op_seq) |
| 6959 | break; |
| 6960 | if (!PL_op_seqmax) |
| 6961 | PL_op_seqmax++; |
| 6962 | PL_op = o; |
| 6963 | switch (o->op_type) { |
| 6964 | case OP_SETSTATE: |
| 6965 | case OP_NEXTSTATE: |
| 6966 | case OP_DBSTATE: |
| 6967 | PL_curcop = ((COP*)o); /* for warnings */ |
| 6968 | o->op_seq = PL_op_seqmax++; |
| 6969 | break; |
| 6970 | |
| 6971 | case OP_CONST: |
| 6972 | if (cSVOPo->op_private & OPpCONST_STRICT) |
| 6973 | no_bareword_allowed(o); |
| 6974 | #ifdef USE_ITHREADS |
| 6975 | /* Relocate sv to the pad for thread safety. |
| 6976 | * Despite being a "constant", the SV is written to, |
| 6977 | * for reference counts, sv_upgrade() etc. */ |
| 6978 | if (cSVOP->op_sv) { |
| 6979 | PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP); |
| 6980 | if (SvPADTMP(cSVOPo->op_sv)) { |
| 6981 | /* If op_sv is already a PADTMP then it is being used by |
| 6982 | * some pad, so make a copy. */ |
| 6983 | sv_setsv(PL_curpad[ix],cSVOPo->op_sv); |
| 6984 | SvREADONLY_on(PL_curpad[ix]); |
| 6985 | SvREFCNT_dec(cSVOPo->op_sv); |
| 6986 | } |
| 6987 | else { |
| 6988 | SvREFCNT_dec(PL_curpad[ix]); |
| 6989 | SvPADTMP_on(cSVOPo->op_sv); |
| 6990 | PL_curpad[ix] = cSVOPo->op_sv; |
| 6991 | /* XXX I don't know how this isn't readonly already. */ |
| 6992 | SvREADONLY_on(PL_curpad[ix]); |
| 6993 | } |
| 6994 | cSVOPo->op_sv = Nullsv; |
| 6995 | o->op_targ = ix; |
| 6996 | } |
| 6997 | #endif |
| 6998 | o->op_seq = PL_op_seqmax++; |
| 6999 | break; |
| 7000 | |
| 7001 | case OP_CONCAT: |
| 7002 | if (o->op_next && o->op_next->op_type == OP_STRINGIFY) { |
| 7003 | if (o->op_next->op_private & OPpTARGET_MY) { |
| 7004 | if (o->op_flags & OPf_STACKED) /* chained concats */ |
| 7005 | goto ignore_optimization; |
| 7006 | else { |
| 7007 | /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */ |
| 7008 | o->op_targ = o->op_next->op_targ; |
| 7009 | o->op_next->op_targ = 0; |
| 7010 | o->op_private |= OPpTARGET_MY; |
| 7011 | } |
| 7012 | } |
| 7013 | op_null(o->op_next); |
| 7014 | } |
| 7015 | ignore_optimization: |
| 7016 | o->op_seq = PL_op_seqmax++; |
| 7017 | break; |
| 7018 | case OP_STUB: |
| 7019 | if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { |
| 7020 | o->op_seq = PL_op_seqmax++; |
| 7021 | break; /* Scalar stub must produce undef. List stub is noop */ |
| 7022 | } |
| 7023 | goto nothin; |
| 7024 | case OP_NULL: |
| 7025 | if (o->op_targ == OP_NEXTSTATE |
| 7026 | || o->op_targ == OP_DBSTATE |
| 7027 | || o->op_targ == OP_SETSTATE) |
| 7028 | { |
| 7029 | PL_curcop = ((COP*)o); |
| 7030 | } |
| 7031 | /* XXX: We avoid setting op_seq here to prevent later calls |
| 7032 | to peep() from mistakenly concluding that optimisation |
| 7033 | has already occurred. This doesn't fix the real problem, |
| 7034 | though (See 20010220.007). AMS 20010719 */ |
| 7035 | if (oldop && o->op_next) { |
| 7036 | oldop->op_next = o->op_next; |
| 7037 | continue; |
| 7038 | } |
| 7039 | break; |
| 7040 | case OP_SCALAR: |
| 7041 | case OP_LINESEQ: |
| 7042 | case OP_SCOPE: |
| 7043 | nothin: |
| 7044 | if (oldop && o->op_next) { |
| 7045 | oldop->op_next = o->op_next; |
| 7046 | continue; |
| 7047 | } |
| 7048 | o->op_seq = PL_op_seqmax++; |
| 7049 | break; |
| 7050 | |
| 7051 | case OP_GV: |
| 7052 | if (o->op_next->op_type == OP_RV2SV) { |
| 7053 | if (!(o->op_next->op_private & OPpDEREF)) { |
| 7054 | op_null(o->op_next); |
| 7055 | o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO |
| 7056 | | OPpOUR_INTRO); |
| 7057 | o->op_next = o->op_next->op_next; |
| 7058 | o->op_type = OP_GVSV; |
| 7059 | o->op_ppaddr = PL_ppaddr[OP_GVSV]; |
| 7060 | } |
| 7061 | } |
| 7062 | else if (o->op_next->op_type == OP_RV2AV) { |
| 7063 | OP* pop = o->op_next->op_next; |
| 7064 | IV i; |
| 7065 | if (pop && pop->op_type == OP_CONST && |
| 7066 | (PL_op = pop->op_next) && |
| 7067 | pop->op_next->op_type == OP_AELEM && |
| 7068 | !(pop->op_next->op_private & |
| 7069 | (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) && |
| 7070 | (i = SvIV(((SVOP*)pop)->op_sv) - PL_curcop->cop_arybase) |
| 7071 | <= 255 && |
| 7072 | i >= 0) |
| 7073 | { |
| 7074 | GV *gv; |
| 7075 | op_null(o->op_next); |
| 7076 | op_null(pop->op_next); |
| 7077 | op_null(pop); |
| 7078 | o->op_flags |= pop->op_next->op_flags & OPf_MOD; |
| 7079 | o->op_next = pop->op_next->op_next; |
| 7080 | o->op_type = OP_AELEMFAST; |
| 7081 | o->op_ppaddr = PL_ppaddr[OP_AELEMFAST]; |
| 7082 | o->op_private = (U8)i; |
| 7083 | gv = cGVOPo_gv; |
| 7084 | GvAVn(gv); |
| 7085 | } |
| 7086 | } |
| 7087 | else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) { |
| 7088 | GV *gv = cGVOPo_gv; |
| 7089 | if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) { |
| 7090 | /* XXX could check prototype here instead of just carping */ |
| 7091 | SV *sv = sv_newmortal(); |
| 7092 | gv_efullname3(sv, gv, Nullch); |
| 7093 | Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), |
| 7094 | "%s() called too early to check prototype", |
| 7095 | SvPV_nolen(sv)); |
| 7096 | } |
| 7097 | } |
| 7098 | else if (o->op_next->op_type == OP_READLINE |
| 7099 | && o->op_next->op_next->op_type == OP_CONCAT |
| 7100 | && (o->op_next->op_next->op_flags & OPf_STACKED)) |
| 7101 | { |
| 7102 | /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */ |
| 7103 | o->op_type = OP_RCATLINE; |
| 7104 | o->op_flags |= OPf_STACKED; |
| 7105 | o->op_ppaddr = PL_ppaddr[OP_RCATLINE]; |
| 7106 | op_null(o->op_next->op_next); |
| 7107 | op_null(o->op_next); |
| 7108 | } |
| 7109 | |
| 7110 | o->op_seq = PL_op_seqmax++; |
| 7111 | break; |
| 7112 | |
| 7113 | case OP_MAPWHILE: |
| 7114 | case OP_GREPWHILE: |
| 7115 | case OP_AND: |
| 7116 | case OP_OR: |
| 7117 | case OP_ANDASSIGN: |
| 7118 | case OP_ORASSIGN: |
| 7119 | case OP_COND_EXPR: |
| 7120 | case OP_RANGE: |
| 7121 | o->op_seq = PL_op_seqmax++; |
| 7122 | while (cLOGOP->op_other->op_type == OP_NULL) |
| 7123 | cLOGOP->op_other = cLOGOP->op_other->op_next; |
| 7124 | peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */ |
| 7125 | break; |
| 7126 | |
| 7127 | case OP_ENTERLOOP: |
| 7128 | case OP_ENTERITER: |
| 7129 | o->op_seq = PL_op_seqmax++; |
| 7130 | while (cLOOP->op_redoop->op_type == OP_NULL) |
| 7131 | cLOOP->op_redoop = cLOOP->op_redoop->op_next; |
| 7132 | peep(cLOOP->op_redoop); |
| 7133 | while (cLOOP->op_nextop->op_type == OP_NULL) |
| 7134 | cLOOP->op_nextop = cLOOP->op_nextop->op_next; |
| 7135 | peep(cLOOP->op_nextop); |
| 7136 | while (cLOOP->op_lastop->op_type == OP_NULL) |
| 7137 | cLOOP->op_lastop = cLOOP->op_lastop->op_next; |
| 7138 | peep(cLOOP->op_lastop); |
| 7139 | break; |
| 7140 | |
| 7141 | case OP_QR: |
| 7142 | case OP_MATCH: |
| 7143 | case OP_SUBST: |
| 7144 | o->op_seq = PL_op_seqmax++; |
| 7145 | while (cPMOP->op_pmreplstart && |
| 7146 | cPMOP->op_pmreplstart->op_type == OP_NULL) |
| 7147 | cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next; |
| 7148 | peep(cPMOP->op_pmreplstart); |
| 7149 | break; |
| 7150 | |
| 7151 | case OP_EXEC: |
| 7152 | o->op_seq = PL_op_seqmax++; |
| 7153 | if (ckWARN(WARN_SYNTAX) && o->op_next |
| 7154 | && o->op_next->op_type == OP_NEXTSTATE) { |
| 7155 | if (o->op_next->op_sibling && |
| 7156 | o->op_next->op_sibling->op_type != OP_EXIT && |
| 7157 | o->op_next->op_sibling->op_type != OP_WARN && |
| 7158 | o->op_next->op_sibling->op_type != OP_DIE) { |
| 7159 | line_t oldline = CopLINE(PL_curcop); |
| 7160 | |
| 7161 | CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next)); |
| 7162 | Perl_warner(aTHX_ packWARN(WARN_EXEC), |
| 7163 | "Statement unlikely to be reached"); |
| 7164 | Perl_warner(aTHX_ packWARN(WARN_EXEC), |
| 7165 | "\t(Maybe you meant system() when you said exec()?)\n"); |
| 7166 | CopLINE_set(PL_curcop, oldline); |
| 7167 | } |
| 7168 | } |
| 7169 | break; |
| 7170 | |
| 7171 | case OP_HELEM: { |
| 7172 | UNOP *rop; |
| 7173 | SV *lexname; |
| 7174 | GV **fields; |
| 7175 | SV **svp, **indsvp, *sv; |
| 7176 | I32 ind; |
| 7177 | char *key = NULL; |
| 7178 | STRLEN keylen; |
| 7179 | |
| 7180 | o->op_seq = PL_op_seqmax++; |
| 7181 | |
| 7182 | if (((BINOP*)o)->op_last->op_type != OP_CONST) |
| 7183 | break; |
| 7184 | |
| 7185 | /* Make the CONST have a shared SV */ |
| 7186 | svp = cSVOPx_svp(((BINOP*)o)->op_last); |
| 7187 | if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) { |
| 7188 | key = SvPV(sv, keylen); |
| 7189 | lexname = newSVpvn_share(key, |
| 7190 | SvUTF8(sv) ? -(I32)keylen : keylen, |
| 7191 | 0); |
| 7192 | SvREFCNT_dec(sv); |
| 7193 | *svp = lexname; |
| 7194 | } |
| 7195 | |
| 7196 | if ((o->op_private & (OPpLVAL_INTRO))) |
| 7197 | break; |
| 7198 | |
| 7199 | rop = (UNOP*)((BINOP*)o)->op_first; |
| 7200 | if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV) |
| 7201 | break; |
| 7202 | lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE); |
| 7203 | if (!(SvFLAGS(lexname) & SVpad_TYPED)) |
| 7204 | break; |
| 7205 | fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE); |
| 7206 | if (!fields || !GvHV(*fields)) |
| 7207 | break; |
| 7208 | key = SvPV(*svp, keylen); |
| 7209 | indsvp = hv_fetch(GvHV(*fields), key, |
| 7210 | SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE); |
| 7211 | if (!indsvp) { |
| 7212 | Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s", |
| 7213 | key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname))); |
| 7214 | } |
| 7215 | ind = SvIV(*indsvp); |
| 7216 | if (ind < 1) |
| 7217 | Perl_croak(aTHX_ "Bad index while coercing array into hash"); |
| 7218 | rop->op_type = OP_RV2AV; |
| 7219 | rop->op_ppaddr = PL_ppaddr[OP_RV2AV]; |
| 7220 | o->op_type = OP_AELEM; |
| 7221 | o->op_ppaddr = PL_ppaddr[OP_AELEM]; |
| 7222 | sv = newSViv(ind); |
| 7223 | if (SvREADONLY(*svp)) |
| 7224 | SvREADONLY_on(sv); |
| 7225 | SvFLAGS(sv) |= (SvFLAGS(*svp) |
| 7226 | & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY)); |
| 7227 | SvREFCNT_dec(*svp); |
| 7228 | *svp = sv; |
| 7229 | break; |
| 7230 | } |
| 7231 | |
| 7232 | case OP_HSLICE: { |
| 7233 | UNOP *rop; |
| 7234 | SV *lexname; |
| 7235 | GV **fields; |
| 7236 | SV **svp, **indsvp, *sv; |
| 7237 | I32 ind; |
| 7238 | char *key; |
| 7239 | STRLEN keylen; |
| 7240 | SVOP *first_key_op, *key_op; |
| 7241 | |
| 7242 | o->op_seq = PL_op_seqmax++; |
| 7243 | if ((o->op_private & (OPpLVAL_INTRO)) |
| 7244 | /* I bet there's always a pushmark... */ |
| 7245 | || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST) |
| 7246 | /* hmmm, no optimization if list contains only one key. */ |
| 7247 | break; |
| 7248 | rop = (UNOP*)((LISTOP*)o)->op_last; |
| 7249 | if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV) |
| 7250 | break; |
| 7251 | lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE); |
| 7252 | if (!(SvFLAGS(lexname) & SVpad_TYPED)) |
| 7253 | break; |
| 7254 | fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE); |
| 7255 | if (!fields || !GvHV(*fields)) |
| 7256 | break; |
| 7257 | /* Again guessing that the pushmark can be jumped over.... */ |
| 7258 | first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling) |
| 7259 | ->op_first->op_sibling; |
| 7260 | /* Check that the key list contains only constants. */ |
| 7261 | for (key_op = first_key_op; key_op; |
| 7262 | key_op = (SVOP*)key_op->op_sibling) |
| 7263 | if (key_op->op_type != OP_CONST) |
| 7264 | break; |
| 7265 | if (key_op) |
| 7266 | break; |
| 7267 | rop->op_type = OP_RV2AV; |
| 7268 | rop->op_ppaddr = PL_ppaddr[OP_RV2AV]; |
| 7269 | o->op_type = OP_ASLICE; |
| 7270 | o->op_ppaddr = PL_ppaddr[OP_ASLICE]; |
| 7271 | for (key_op = first_key_op; key_op; |
| 7272 | key_op = (SVOP*)key_op->op_sibling) { |
| 7273 | svp = cSVOPx_svp(key_op); |
| 7274 | key = SvPV(*svp, keylen); |
| 7275 | indsvp = hv_fetch(GvHV(*fields), key, |
| 7276 | SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE); |
| 7277 | if (!indsvp) { |
| 7278 | Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" " |
| 7279 | "in variable %s of type %s", |
| 7280 | key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname))); |
| 7281 | } |
| 7282 | ind = SvIV(*indsvp); |
| 7283 | if (ind < 1) |
| 7284 | Perl_croak(aTHX_ "Bad index while coercing array into hash"); |
| 7285 | sv = newSViv(ind); |
| 7286 | if (SvREADONLY(*svp)) |
| 7287 | SvREADONLY_on(sv); |
| 7288 | SvFLAGS(sv) |= (SvFLAGS(*svp) |
| 7289 | & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY)); |
| 7290 | SvREFCNT_dec(*svp); |
| 7291 | *svp = sv; |
| 7292 | } |
| 7293 | break; |
| 7294 | } |
| 7295 | |
| 7296 | default: |
| 7297 | o->op_seq = PL_op_seqmax++; |
| 7298 | break; |
| 7299 | } |
| 7300 | oldop = o; |
| 7301 | } |
| 7302 | LEAVE; |
| 7303 | } |
| 7304 | |
| 7305 | |
| 7306 | |
| 7307 | char* Perl_custom_op_name(pTHX_ OP* o) |
| 7308 | { |
| 7309 | IV index = PTR2IV(o->op_ppaddr); |
| 7310 | SV* keysv; |
| 7311 | HE* he; |
| 7312 | |
| 7313 | if (!PL_custom_op_names) /* This probably shouldn't happen */ |
| 7314 | return PL_op_name[OP_CUSTOM]; |
| 7315 | |
| 7316 | keysv = sv_2mortal(newSViv(index)); |
| 7317 | |
| 7318 | he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0); |
| 7319 | if (!he) |
| 7320 | return PL_op_name[OP_CUSTOM]; /* Don't know who you are */ |
| 7321 | |
| 7322 | return SvPV_nolen(HeVAL(he)); |
| 7323 | } |
| 7324 | |
| 7325 | char* Perl_custom_op_desc(pTHX_ OP* o) |
| 7326 | { |
| 7327 | IV index = PTR2IV(o->op_ppaddr); |
| 7328 | SV* keysv; |
| 7329 | HE* he; |
| 7330 | |
| 7331 | if (!PL_custom_op_descs) |
| 7332 | return PL_op_desc[OP_CUSTOM]; |
| 7333 | |
| 7334 | keysv = sv_2mortal(newSViv(index)); |
| 7335 | |
| 7336 | he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0); |
| 7337 | if (!he) |
| 7338 | return PL_op_desc[OP_CUSTOM]; |
| 7339 | |
| 7340 | return SvPV_nolen(HeVAL(he)); |
| 7341 | } |
| 7342 | |
| 7343 | |
| 7344 | #include "XSUB.h" |
| 7345 | |
| 7346 | /* Efficient sub that returns a constant scalar value. */ |
| 7347 | static void |
| 7348 | const_sv_xsub(pTHX_ CV* cv) |
| 7349 | { |
| 7350 | dXSARGS; |
| 7351 | if (items != 0) { |
| 7352 | #if 0 |
| 7353 | Perl_croak(aTHX_ "usage: %s::%s()", |
| 7354 | HvNAME(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv))); |
| 7355 | #endif |
| 7356 | } |
| 7357 | EXTEND(sp, 1); |
| 7358 | ST(0) = (SV*)XSANY.any_ptr; |
| 7359 | XSRETURN(1); |
| 7360 | } |