tests for redefined subroutine warnings.
[p5sagit/Devel-Declare.git] / Declare.xs
CommitLineData
94caac6e 1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
b15aa864 4#include "hook_op_check.h"
94caac6e 5#undef printf
e807ee50 6#include "stolen_chunk_of_toke.c"
94caac6e 7#include <stdio.h>
8#include <string.h>
9
023db2fb 10#define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
11#define PERL_DECIMAL_VERSION \
12 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
13#define PERL_VERSION_GE(r,v,s) \
14 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
15
9673d7ca 16#ifndef Newx
17# define Newx(v,n,t) New(0,v,n,t)
18#endif /* !Newx */
19
7dd7d008 20#define DD_DEBUGf_UPDATED_LINESTR 1
21#define DD_DEBUGf_TRACE 2
22
23#define DD_DEBUG_UPDATED_LINESTR (dd_debug & DD_DEBUGf_UPDATED_LINESTR)
24#define DD_DEBUG_TRACE (dd_debug & DD_DEBUGf_TRACE)
e81bee92 25static int dd_debug = 0;
c630715a 26
023db2fb 27#define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)
28
6ed8c948 29#define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)
30
94caac6e 31#define LEX_NORMAL 10
32#define LEX_INTERPNORMAL 9
33
94caac6e 34/* flag to trigger removal of temporary declaree sub */
35
36static int in_declare = 0;
37
96f12726 38/* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
39 is a lookup into it - so if anything else we can use to tell, so we
40 need to be a bit more careful if PL_parser exists */
41
42#define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
43
f19b3507 44#if defined(PL_parser) || defined(PERL_5_9_PLUS)
96f12726 45#define DD_HAVE_PARSER PL_parser
46#define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
47#define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
48#else
49#define DD_HAVE_PARSER 1
50#define DD_HAVE_LEX_STUFF PL_lex_stuff
51#define DD_AM_LEXING DD_AM_LEXING_CHECK
52#endif
53
72f20f69 54/* thing that decides whether we're dealing with a declarator */
55
56int dd_is_declarator(pTHX_ char* name) {
57 HV* is_declarator;
58 SV** is_declarator_pack_ref;
59 HV* is_declarator_pack_hash;
60 SV** is_declarator_flag_ref;
61 int dd_flags;
62
63 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
64
65 if (!is_declarator)
66 return -1;
67
569ac469 68 /* $declarators{$current_package_name} */
69
9603b8dc 70 if (!HvNAME(PL_curstash))
f2a41aa1 71 return -1;
9603b8dc 72
72f20f69 73 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
74 strlen(HvNAME(PL_curstash)), FALSE);
75
76 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
77 return -1; /* not a hashref */
78
79 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
80
569ac469 81 /* $declarators{$current_package_name}{$name} */
82
72f20f69 83 is_declarator_flag_ref = hv_fetch(
84 is_declarator_pack_hash, name,
85 strlen(name), FALSE
86 );
87
88 /* requires SvIOK as well as TRUE since flags not being an int is useless */
89
90 if (!is_declarator_flag_ref
48ee5c99 91 || !SvIOK(*is_declarator_flag_ref)
72f20f69 92 || !SvTRUE(*is_declarator_flag_ref))
93 return -1;
94
95 dd_flags = SvIVX(*is_declarator_flag_ref);
96
97 return dd_flags;
98}
99
569ac469 100/* callback thingy */
101
a9bd9b5e 102void dd_linestr_callback (pTHX_ char* type, char* name) {
569ac469 103
104 char* linestr = SvPVX(PL_linestr);
a9bd9b5e 105 int offset = PL_bufptr - linestr;
569ac469 106
569ac469 107 dSP;
108
109 ENTER;
110 SAVETMPS;
111
112 PUSHMARK(SP);
113 XPUSHs(sv_2mortal(newSVpv(type, 0)));
114 XPUSHs(sv_2mortal(newSVpv(name, 0)));
115 XPUSHs(sv_2mortal(newSViv(offset)));
116 PUTBACK;
117
04a8a223 118 call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
569ac469 119
569ac469 120 FREETMPS;
121 LEAVE;
122}
123
124char* dd_get_linestr(pTHX) {
5f0b59d5 125 if (!DD_HAVE_PARSER) {
126 return NULL;
127 }
569ac469 128 return SvPVX(PL_linestr);
129}
130
131void dd_set_linestr(pTHX_ char* new_value) {
6f5220b7 132 unsigned int new_len = strlen(new_value);
569ac469 133
ce9a252b 134 if (SvLEN(PL_linestr) < new_len) {
41db92e3 135 croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
136 CopFILE(&PL_compiling)
137 );
ce9a252b 138 }
139
569ac469 140
141 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
142
143 SvCUR_set(PL_linestr, new_len);
144
145 PL_bufend = SvPVX(PL_linestr) + new_len;
7dd7d008 146
87195072 147 if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
7dd7d008 148 // Cribbed from toke.c
149 SV * const sv = NEWSV(85,0);
150
151 sv_upgrade(sv, SVt_PVMG);
152 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
153 (void)SvIOK_on(sv);
154 SvIV_set(sv, 0);
155 av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
156 }
569ac469 157}
158
04a8a223 159char* dd_get_lex_stuff(pTHX) {
96f12726 160 return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
04a8a223 161}
162
bd85a06e 163void dd_clear_lex_stuff(pTHX) {
96f12726 164 if (DD_HAVE_PARSER)
f05cbc90 165 PL_lex_stuff = (SV*)NULL;
04a8a223 166}
167
168char* dd_get_curstash_name(pTHX) {
169 return HvNAME(PL_curstash);
170}
171
022eb0cc 172int dd_get_linestr_offset(pTHX) {
0da63271 173 char* linestr;
174 if (!DD_HAVE_PARSER) {
175 return -1;
176 }
177 linestr = SvPVX(PL_linestr);
022eb0cc 178 return PL_bufptr - linestr;
179}
180
840ebcbb 181char* dd_move_past_token (pTHX_ char* s) {
923c07a8 182
183 /*
184 * buffer will be at the beginning of the declarator, -unless- the
185 * declarator is at EOL in which case it'll be the next useful line
186 * so we don't short-circuit out if we don't find the declarator
187 */
188
189 while (s < PL_bufend && isSPACE(*s)) s++;
190 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
191 s += strlen(PL_tokenbuf);
192 return s;
193}
194
840ebcbb 195int dd_toke_move_past_token (pTHX_ int offset) {
04a8a223 196 char* base_s = SvPVX(PL_linestr) + offset;
197 char* s = dd_move_past_token(aTHX_ base_s);
198 return s - base_s;
199}
200
923c07a8 201int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
202 char tmpbuf[sizeof PL_tokenbuf];
203 char* base_s = SvPVX(PL_linestr) + offset;
204 STRLEN len;
205 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
206 return s - base_s;
207}
208
f11d21b2 209int dd_toke_scan_ident(pTHX_ int offset) {
210 char tmpbuf[sizeof PL_tokenbuf];
211 char* base_s = SvPVX(PL_linestr) + offset;
f11d21b2 212 char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
213 return s - base_s;
214}
215
923c07a8 216int dd_toke_scan_str(pTHX_ int offset) {
19b277fc 217 STRLEN remaining = sv_len(PL_linestr) - offset;
86964fb3 218 SV* line_copy = newSVsv(PL_linestr);
923c07a8 219 char* base_s = SvPVX(PL_linestr) + offset;
220 char* s = scan_str(base_s, FALSE, FALSE);
86964fb3 221 if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
222 int ret = (s - SvPVX(PL_linestr)) + remaining;
223 sv_catsv(line_copy, PL_linestr);
224 dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
225 SvREFCNT_dec(line_copy);
226 return ret;
227 }
923c07a8 228 return s - base_s;
229}
230
231int dd_toke_skipspace(pTHX_ int offset) {
232 char* base_s = SvPVX(PL_linestr) + offset;
a25db2dc 233 char* s = skipspace_force(base_s);
923c07a8 234 return s - base_s;
235}
236
023db2fb 237static void call_done_declare(pTHX) {
238 dSP;
239
240 if (DD_DEBUG_TRACE) {
241 printf("Deconstructing declare\n");
242 printf("PL_bufptr: %s\n", PL_bufptr);
243 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
244 printf("linestr: %s\n", SvPVX(PL_linestr));
245 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
246 }
247
248 ENTER;
249 SAVETMPS;
250
251 PUSHMARK(SP);
252
253 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
254
255 FREETMPS;
256 LEAVE;
257
258 if (DD_DEBUG_TRACE) {
259 printf("PL_bufptr: %s\n", PL_bufptr);
260 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
261 printf("linestr: %s\n", SvPVX(PL_linestr));
262 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
263 printf("actual len: %i\n", strlen(PL_bufptr));
264 }
265}
266
267static int dd_handle_const(pTHX_ char *name);
268
94caac6e 269/* replacement PL_check rv2cv entry */
270
6e67754a 271STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
94caac6e 272 OP* kid;
0ba8c7aa 273 int dd_flags;
94caac6e 274
6f5220b7 275 PERL_UNUSED_VAR(user_data);
276
499109e0 277 if (!DD_AM_LEXING)
278 return o; /* not lexing? */
279
94caac6e 280 if (in_declare) {
023db2fb 281 call_done_declare(aTHX);
94caac6e 282 return o;
283 }
284
285 kid = cUNOPo->op_first;
286
287 if (kid->op_type != OP_GV) /* not a GV so ignore */
288 return o;
289
7dd7d008 290 if (DD_DEBUG_TRACE) {
e81bee92 291 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
292 }
840ebcbb 293
72f20f69 294 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
94caac6e 295
72f20f69 296 if (dd_flags == -1)
94caac6e 297 return o;
298
7dd7d008 299 if (DD_DEBUG_TRACE) {
e81bee92 300 printf("dd_flags are: %i\n", dd_flags);
301 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
302 }
840ebcbb 303
023db2fb 304#if DD_CONST_VIA_RV2CV
305 if (PL_expect != XOPERATOR) {
306 if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
307 return o;
308 CopLINE(PL_curcop) = PL_copline;
309 /* The parser behaviour that we're simulating depends on what comes
310 after the declarator. */
311 if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
312 if (in_declare) {
313 call_done_declare(aTHX);
314 } else {
315 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
316 }
317 }
318 return o;
319 }
320#endif /* DD_CONST_VIA_RV2CV */
321
a9bd9b5e 322 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
94caac6e 323
53e3ab32 324 return o;
325}
326
6ed8c948 327#if DD_GROW_VIA_BLOCKHOOK
328
329static void dd_block_start(pTHX_ int full)
330{
331 PERL_UNUSED_VAR(full);
332 if (SvLEN(PL_linestr) < 8192)
333 (void) lex_grow_linestr(8192);
334}
335
336#else /* !DD_GROW_VIA_BLOCKHOOK */
337
b7505981 338OP* dd_pp_entereval(pTHX) {
339 dSP;
b7505981 340 STRLEN len;
341 const char* s;
c93bc23e 342 SV *sv;
343#ifdef PERL_5_9_PLUS
344 SV *saved_hh;
345 if (PL_op->op_private & OPpEVAL_HAS_HH) {
346 saved_hh = POPs;
347 }
348#endif
349 sv = POPs;
b7505981 350 if (SvPOK(sv)) {
7dd7d008 351 if (DD_DEBUG_TRACE) {
e81bee92 352 printf("mangling eval sv\n");
353 }
b7505981 354 if (SvREADONLY(sv))
355 sv = sv_2mortal(newSVsv(sv));
356 s = SvPVX(sv);
357 len = SvCUR(sv);
358 if (!len || s[len-1] != ';') {
359 if (!(SvFLAGS(sv) & SVs_TEMP))
360 sv = sv_2mortal(newSVsv(sv));
361 sv_catpvn(sv, "\n;", 2);
53e3ab32 362 }
b7505981 363 SvGROW(sv, 8192);
53e3ab32 364 }
b7505981 365 PUSHs(sv);
c93bc23e 366#ifdef PERL_5_9_PLUS
367 if (PL_op->op_private & OPpEVAL_HAS_HH) {
368 PUSHs(saved_hh);
369 }
370#endif
b7505981 371 return PL_ppaddr[OP_ENTEREVAL](aTHX);
372}
53e3ab32 373
6e67754a 374STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
6f5220b7 375 PERL_UNUSED_VAR(user_data);
376
d8e65fc8 377 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
378 o->op_ppaddr = dd_pp_entereval;
94caac6e 379 return o;
380}
381
12b60feb 382#endif /* !DD_GROW_VIA_BLOCKHOOK */
383
6a0bcf35 384static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
385{
386 const I32 count = FILTER_READ(idx+1, sv, maxlen);
387 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
388 /* filter_del(dd_filter_realloc); */
389 return count;
390}
391
023db2fb 392static int dd_handle_const(pTHX_ char *name) {
c48ac26f 393 switch (PL_lex_inwhat) {
394 case OP_QR:
395 case OP_MATCH:
396 case OP_SUBST:
397 case OP_TRANS:
f2a41aa1 398 case OP_BACKTICK:
399 case OP_STRINGIFY:
023db2fb 400 return 0;
c48ac26f 401 break;
402 default:
403 break;
8d96afb7 404 }
48ee5c99 405
a25db2dc 406 if (strnEQ(PL_bufptr, "->", 2)) {
023db2fb 407 return 0;
954da332 408 }
409
a25db2dc 410 {
411 char buf[256];
412 STRLEN len;
413 char *s = PL_bufptr;
414 STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
415
416 s = scan_word(s, buf, sizeof buf, FALSE, &len);
417 if (strnEQ(buf, name, len)) {
418 char *d;
c0439e97 419 SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
a25db2dc 420 sv_catpvn(inject, buf, len);
421
422 d = peekspace(s);
423 sv_catpvn(inject, s, d - s);
424
425 if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
023db2fb 426 return 0;
a25db2dc 427 }
428
429 sv_catpv(inject, d);
430 dd_set_linestr(aTHX_ SvPV_nolen(inject));
431 PL_bufptr = SvPVX(PL_linestr) + old_offset;
432 SvREFCNT_dec (inject);
433 }
434 }
435
a9bd9b5e 436 dd_linestr_callback(aTHX_ "const", name);
bedac9ff 437
023db2fb 438 return 1;
439}
440
441#if !DD_CONST_VIA_RV2CV
442
443STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
444 int dd_flags;
445 char* name;
446
447 PERL_UNUSED_VAR(user_data);
448
449 if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
450 return o;
451 }
452
453 /* if this is set, we just grabbed a delimited string or something,
454 not a bareword, so NO TOUCHY */
455
456 if (DD_HAVE_LEX_STUFF)
457 return o;
458
459 /* don't try and look this up if it's not a string const */
460 if (!SvPOK(cSVOPo->op_sv))
461 return o;
462
463 name = SvPVX(cSVOPo->op_sv);
464
465 dd_flags = dd_is_declarator(aTHX_ name);
466
467 if (dd_flags == -1)
468 return o;
469
470 dd_handle_const(aTHX_ name);
471
3ea50944 472 return o;
473}
474
023db2fb 475#endif /* !DD_CONST_VIA_RV2CV */
476
94caac6e 477static int initialized = 0;
478
479MODULE = Devel::Declare PACKAGE = Devel::Declare
480
481PROTOTYPES: DISABLE
482
483void
484setup()
485 CODE:
486 if (!initialized++) {
6ed8c948 487#if DD_GROW_VIA_BLOCKHOOK
488 static BHK bhk;
489#if PERL_VERSION_GE(5,13,6)
490 BhkENTRY_set(&bhk, bhk_start, dd_block_start);
491#else /* <5.13.6 */
492 BhkENTRY_set(&bhk, start, dd_block_start);
493#endif /* <5.13.6 */
494 Perl_blockhook_register(aTHX_ &bhk);
495#else /* !DD_GROW_VIA_BLOCKHOOK */
6e67754a 496 hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
6ed8c948 497#endif /* !DD_GROW_VIA_BLOCKHOOK */
498 hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
023db2fb 499#if !DD_CONST_VIA_RV2CV
6e67754a 500 hook_op_check(OP_CONST, dd_ck_const, NULL);
023db2fb 501#endif /* !DD_CONST_VIA_RV2CV */
94caac6e 502 }
6a0bcf35 503 filter_add(dd_filter_realloc, NULL);
569ac469 504
505char*
506get_linestr()
507 CODE:
508 RETVAL = dd_get_linestr(aTHX);
509 OUTPUT:
510 RETVAL
923c07a8 511
512void
513set_linestr(char* new_value)
514 CODE:
515 dd_set_linestr(aTHX_ new_value);
516
04a8a223 517char*
518get_lex_stuff()
519 CODE:
520 RETVAL = dd_get_lex_stuff(aTHX);
521 OUTPUT:
522 RETVAL
523
524void
525clear_lex_stuff()
526 CODE:
527 dd_clear_lex_stuff(aTHX);
528
529char*
530get_curstash_name()
531 CODE:
532 RETVAL = dd_get_curstash_name(aTHX);
533 OUTPUT:
534 RETVAL
535
923c07a8 536int
022eb0cc 537get_linestr_offset()
538 CODE:
539 RETVAL = dd_get_linestr_offset(aTHX);
540 OUTPUT:
541 RETVAL
542
543int
923c07a8 544toke_scan_word(int offset, int handle_package)
545 CODE:
546 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
547 OUTPUT:
548 RETVAL
549
550int
04a8a223 551toke_move_past_token(int offset);
552 CODE:
553 RETVAL = dd_toke_move_past_token(aTHX_ offset);
554 OUTPUT:
555 RETVAL
556
557int
923c07a8 558toke_scan_str(int offset);
559 CODE:
560 RETVAL = dd_toke_scan_str(aTHX_ offset);
561 OUTPUT:
562 RETVAL
563
564int
f11d21b2 565toke_scan_ident(int offset)
566 CODE:
567 RETVAL = dd_toke_scan_ident(aTHX_ offset);
568 OUTPUT:
569 RETVAL
570
571int
923c07a8 572toke_skipspace(int offset)
573 CODE:
574 RETVAL = dd_toke_skipspace(aTHX_ offset);
575 OUTPUT:
576 RETVAL
04a8a223 577
578int
579get_in_declare()
580 CODE:
581 RETVAL = in_declare;
582 OUTPUT:
583 RETVAL
584
585void
586set_in_declare(int value)
587 CODE:
588 in_declare = value;
e81bee92 589
590BOOT:
0a3b37d1 591{
87195072 592 char *endptr;
593 char *debug_str = getenv ("DD_DEBUG");
594 if (debug_str) {
595 dd_debug = strtol (debug_str, &endptr, 10);
596 if (*endptr != '\0') {
597 dd_debug = 0;
598 }
e81bee92 599 }
0a3b37d1 600}