File: Declare.xs

package info (click to toggle)
libdevel-declare-perl 0.006019-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 376 kB
  • ctags: 150
  • sloc: ansic: 752; perl: 608; makefile: 3
file content (666 lines) | stat: -rw-r--r-- 15,604 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#define PERL_NO_GET_CONTEXT 1
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "hook_op_check.h"
#undef printf
#include "stolen_chunk_of_toke.c"
#include <stdio.h>
#include <string.h>

#define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#define PERL_DECIMAL_VERSION \
  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#define PERL_VERSION_GE(r,v,s) \
  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))

#ifndef Newx
# define Newx(v,n,t) New(0,v,n,t)
#endif /* !Newx */

#define DD_DEBUGf_UPDATED_LINESTR 1
#define DD_DEBUGf_TRACE 2

#define DD_DEBUG_UPDATED_LINESTR (dd_debug & DD_DEBUGf_UPDATED_LINESTR)
#define DD_DEBUG_TRACE (dd_debug & DD_DEBUGf_TRACE)
static int dd_debug = 0;

#define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)

#define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)

#define LEX_NORMAL    10
#define LEX_INTERPNORMAL   9

/* please try not to have a line longer than this :) */

#define DD_PREFERRED_LINESTR_SIZE 16384

/* flag to trigger removal of temporary declaree sub */

static int in_declare = 0;

/* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
   is a lookup into it - so if anything else we can use to tell, so we
   need to be a bit more careful if PL_parser exists */

#define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)

#if defined(PL_parser) || defined(PERL_5_9_PLUS)
#define DD_HAVE_PARSER PL_parser
#define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
#define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
#else
#define DD_HAVE_PARSER 1
#define DD_HAVE_LEX_STUFF PL_lex_stuff
#define DD_AM_LEXING DD_AM_LEXING_CHECK
#endif

/* thing that decides whether we're dealing with a declarator */

int dd_is_declarator(pTHX_ char* name) {
  HV* is_declarator;
  SV** is_declarator_pack_ref;
  HV* is_declarator_pack_hash;
  SV** is_declarator_flag_ref;
  int dd_flags;
  char* curstash_name;

  is_declarator = get_hv("Devel::Declare::declarators", FALSE);

  if (!is_declarator)
    return -1;

  /* $declarators{$current_package_name} */

  curstash_name = HvNAME(PL_curstash);
  if (!curstash_name)
    return -1;

  is_declarator_pack_ref = hv_fetch(is_declarator, curstash_name,
                             strlen(curstash_name), FALSE);

  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
    return -1; /* not a hashref */

  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);

  /* $declarators{$current_package_name}{$name} */

  is_declarator_flag_ref = hv_fetch(
    is_declarator_pack_hash, name,
    strlen(name), FALSE
  );

  /* requires SvIOK as well as TRUE since flags not being an int is useless */

  if (!is_declarator_flag_ref
        || !SvIOK(*is_declarator_flag_ref)
        || !SvTRUE(*is_declarator_flag_ref))
    return -1;

  dd_flags = SvIVX(*is_declarator_flag_ref);

  return dd_flags;
}

/* callback thingy */

void dd_linestr_callback (pTHX_ char* type, char* name) {

  char* linestr = SvPVX(PL_linestr);
  int offset = PL_bufptr - linestr;

  dSP;

  ENTER;
  SAVETMPS;

  PUSHMARK(SP);
  XPUSHs(sv_2mortal(newSVpv(type, 0)));
  XPUSHs(sv_2mortal(newSVpv(name, 0)));
  XPUSHs(sv_2mortal(newSViv(offset)));
  PUTBACK;

  call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);

  FREETMPS;
  LEAVE;
}

char* dd_get_linestr(pTHX) {
  if (!DD_HAVE_PARSER) {
    return NULL;
  }
  return SvPVX(PL_linestr);
}

void dd_set_linestr(pTHX_ char* new_value) {
  unsigned int new_len = strlen(new_value);

  if (SvLEN(PL_linestr) < new_len) {
    croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
      CopFILE(&PL_compiling)
    );
  }


  memcpy(SvPVX(PL_linestr), new_value, new_len+1);

  SvCUR_set(PL_linestr, new_len);

  PL_bufend = SvPVX(PL_linestr) + new_len;

  if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
    /* Cribbed from toke.c */
    AV *fileav = CopFILEAV(&PL_compiling);
    if (fileav) {
      SV * const sv = NEWSV(85,0);

      sv_upgrade(sv, SVt_PVMG);
      sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
      (void)SvIOK_on(sv);
      SvIV_set(sv, 0);
      av_store(fileav,(I32)CopLINE(&PL_compiling),sv);
    }
  }
}

char* dd_get_lex_stuff(pTHX) {
  return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
}

void dd_clear_lex_stuff(pTHX) {
  if (DD_HAVE_PARSER)
    PL_lex_stuff = (SV*)NULL;
}

char* dd_get_curstash_name(pTHX) {
  return HvNAME(PL_curstash);
}

int dd_get_linestr_offset(pTHX) {
  char* linestr;
  if (!DD_HAVE_PARSER) {
    return -1;
  }
  linestr = SvPVX(PL_linestr);
  return PL_bufptr - linestr;
}

char* dd_move_past_token (pTHX_ char* s) {

  /*
   *   buffer will be at the beginning of the declarator, -unless- the
   *   declarator is at EOL in which case it'll be the next useful line
   *   so we don't short-circuit out if we don't find the declarator
   */

  while (s < PL_bufend && isSPACE(*s)) s++;
  if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
    s += strlen(PL_tokenbuf);
  return s;
}

int dd_toke_move_past_token (pTHX_ int offset) {
  char* base_s = SvPVX(PL_linestr) + offset;
  char* s = dd_move_past_token(aTHX_ base_s);
  return s - base_s;
}

int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
  char tmpbuf[sizeof PL_tokenbuf];
  char* base_s = SvPVX(PL_linestr) + offset;
  STRLEN len;
  char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
  return s - base_s;
}

int dd_toke_scan_ident(pTHX_ int offset) {
    char tmpbuf[sizeof PL_tokenbuf];
    char* base_s = SvPVX(PL_linestr) + offset;
    char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
    return s - base_s;
}

int dd_toke_scan_str(pTHX_ int offset) {
  char* old_pvx = SvPVX(PL_linestr);
  SV* line_copy = sv_2mortal(newSVsv(PL_linestr));
  char* base_s = SvPVX(PL_linestr) + offset;
  char* s = scan_str(base_s, FALSE, FALSE);
  if(SvPVX(PL_linestr) != old_pvx)
    croak("PL_linestr reallocated during scan_str, "
      "Devel::Declare can't continue");
  if (!s)
    return 0;
  if (s <= base_s || memcmp(SvPVX(line_copy), SvPVX(PL_linestr), offset)) {
    s += SvCUR(line_copy);
    sv_catsv(line_copy, PL_linestr);
    dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
  }
  return s - base_s;
}

int dd_toke_skipspace(pTHX_ int offset) {
  char* old_pvx = SvPVX(PL_linestr);
  char* base_s = SvPVX(PL_linestr) + offset;
  char* s = skipspace_force(base_s);
  if(SvPVX(PL_linestr) != old_pvx)
    croak("PL_linestr reallocated during skipspace, "
      "Devel::Declare can't continue");
  return s - base_s;
}

static void call_done_declare(pTHX) {
  dSP;

  if (DD_DEBUG_TRACE) {
    printf("Deconstructing declare\n");
    printf("PL_bufptr: %s\n", PL_bufptr);
    printf("bufend at: %i\n", (int)(PL_bufend - PL_bufptr));
    printf("linestr: %s\n", SvPVX(PL_linestr));
    printf("linestr len: %i\n", (int)(PL_bufend - SvPVX(PL_linestr)));
  }

  ENTER;
  SAVETMPS;

  PUSHMARK(SP);

  call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);

  FREETMPS;
  LEAVE;

  if (DD_DEBUG_TRACE) {
    printf("PL_bufptr: %s\n", PL_bufptr);
    printf("bufend at: %i\n", (int)(PL_bufend - PL_bufptr));
    printf("linestr: %s\n", SvPVX(PL_linestr));
    printf("linestr len: %i\n", (int)(PL_bufend - SvPVX(PL_linestr)));
    printf("actual len: %i\n", (int)strlen(PL_bufptr));
  }
}

static int dd_handle_const(pTHX_ char *name);

#ifdef CV_NAME_NOTQUAL /* 5.21.5 */
# define Gv_or_CvNAME(g) (isGV(g) \
               ? GvNAME(g) \
               : SvPV_nolen(cv_name((CV *)SvRV(g), NULL, CV_NAME_NOTQUAL)))
#elif defined(CvNAMED) /* 5.21.4 */
# define Gv_or_CvNAME(g) (isGV(g) \
                            ? GvNAME(g) \
                            : CvNAMED(SvRV(g)) \
                                ? HEK_KEY(CvNAME_HEK((CV *)SvRV(g))) \
                                : GvNAME(CvGV(SvRV(g))))
#else
# define Gv_or_CvNAME(g) GvNAME(g)
#endif

/* replacement PL_check rv2cv entry */

STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
  OP* kid;
  int dd_flags;
  char *gvname;

  PERL_UNUSED_VAR(user_data);

  if (!DD_AM_LEXING)
    return o; /* not lexing? */

  if (in_declare) {
    call_done_declare(aTHX);
    return o;
  }

  kid = cUNOPo->op_first;

  if (kid->op_type != OP_GV) /* not a GV so ignore */
    return o;

  if (!isGV(kGVOP_gv)
   && (!SvROK(kGVOP_gv) || SvTYPE(SvRV(kGVOP_gv)) != SVt_PVCV))
    return o;

  gvname = Gv_or_CvNAME(kGVOP_gv);

  if (DD_DEBUG_TRACE) {
    printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), gvname);
  }

  dd_flags = dd_is_declarator(aTHX_ gvname);

  if (dd_flags == -1)
    return o;

  if (DD_DEBUG_TRACE) {
    printf("dd_flags are: %i\n", dd_flags);
    printf("PL_tokenbuf: %s\n", PL_tokenbuf);
  }

#if DD_CONST_VIA_RV2CV
  if (PL_expect != XOPERATOR) {
    if (!dd_handle_const(aTHX_ Gv_or_CvNAME(kGVOP_gv)))
      return o;
    CopLINE(PL_curcop) = PL_copline;
    /* The parser behaviour that we're simulating depends on what comes
       after the declarator. */
    if (*skipspace(PL_bufptr + strlen(gvname)) != '(') {
      if (in_declare) {
        call_done_declare(aTHX);
      } else {
        dd_linestr_callback(aTHX_ "rv2cv", gvname);
      }
    }
    return o;
  }
#endif /* DD_CONST_VIA_RV2CV */

  dd_linestr_callback(aTHX_ "rv2cv", gvname);

  return o;
}

#if DD_GROW_VIA_BLOCKHOOK

static void dd_block_start(pTHX_ int full)
{
  PERL_UNUSED_VAR(full);
  if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
    (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
}

#else /* !DD_GROW_VIA_BLOCKHOOK */

OP* dd_pp_entereval(pTHX) {
  dSP;
  STRLEN len;
  const char* s;
  SV *sv;
#ifdef PERL_5_9_PLUS
  SV *saved_hh = NULL;
  if (PL_op->op_private & OPpEVAL_HAS_HH) {
    saved_hh = POPs;
  }
#endif
  sv = POPs;
  if (SvPOK(sv)) {
    if (DD_DEBUG_TRACE) {
      printf("mangling eval sv\n");
    }
    if (SvREADONLY(sv))
      sv = sv_2mortal(newSVsv(sv));
    s = SvPVX(sv);
    len = SvCUR(sv);
    if (!len || s[len-1] != ';') {
      if (!(SvFLAGS(sv) & SVs_TEMP))
        sv = sv_2mortal(newSVsv(sv));
      sv_catpvn(sv, "\n;", 2);
    }
    SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
  }
  PUSHs(sv);
#ifdef PERL_5_9_PLUS
  if (PL_op->op_private & OPpEVAL_HAS_HH) {
    PUSHs(saved_hh);
  }
#endif
  return PL_ppaddr[OP_ENTEREVAL](aTHX);
}

STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
  PERL_UNUSED_VAR(user_data);

  if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
    o->op_ppaddr = dd_pp_entereval;
  return o;
}

#endif /* !DD_GROW_VIA_BLOCKHOOK */

static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
{
  SV *filter_datasv;
  const I32 count = FILTER_READ(idx+1, sv, maxlen);
  SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
  /* Filters can only be deleted in the correct order (reverse of the
     order in which they were added).  Insisting on deleting the filter
     here would break if another filter were added after ours and is
     still around.  Not deleting the filter at all would break if another
     filter were added earlier and attempts to delete itself later.
     We can play nicely to the maximum possible extent by deleting our
     filter iff it is currently deletable (i.e., it is on the top of
     the filter stack).  Can still run into trouble in more complex
     situations, but can't avoid that. */
  if (PL_rsfp_filters && AvFILLp(PL_rsfp_filters) >= 0 &&
      (filter_datasv = FILTER_DATA(AvFILLp(PL_rsfp_filters))) &&
      IoANY(filter_datasv) == FPTR2DPTR(void *, dd_filter_realloc)) {
    filter_del(dd_filter_realloc);
  }
  return count;
}

static int dd_handle_const(pTHX_ char *name) {
  switch (PL_lex_inwhat) {
    case OP_QR:
    case OP_MATCH:
    case OP_SUBST:
    case OP_TRANS:
    case OP_BACKTICK:
    case OP_STRINGIFY:
      return 0;
      break;
    default:
      break;
  }

  if (strnEQ(PL_bufptr, "->", 2)) {
    return 0;
  }

  {
    char buf[256];
    STRLEN len;
    char *s = PL_bufptr;
    STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);

    s = scan_word(s, buf, sizeof buf, FALSE, &len);
    if (strnEQ(buf, name, len)) {
      char *d;
      SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
      sv_catpvn(inject, buf, len);

      d = peekspace(s);
      sv_catpvn(inject, s, d - s);

      if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
        return 0;
      }

      sv_catpv(inject, d);
      dd_set_linestr(aTHX_ SvPV_nolen(inject));
      PL_bufptr = SvPVX(PL_linestr) + old_offset;
      SvREFCNT_dec (inject);
    }
  }

  dd_linestr_callback(aTHX_ "const", name);

  return 1;
}

#if !DD_CONST_VIA_RV2CV

STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
  int dd_flags;
  char* name;

  PERL_UNUSED_VAR(user_data);

  if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
    return o;
  }

  /* if this is set, we just grabbed a delimited string or something,
     not a bareword, so NO TOUCHY */

  if (DD_HAVE_LEX_STUFF)
    return o;

  /* don't try and look this up if it's not a string const */
  if (!SvPOK(cSVOPo->op_sv))
    return o;

  name = SvPVX(cSVOPo->op_sv);

  dd_flags = dd_is_declarator(aTHX_ name);

  if (dd_flags == -1)
    return o;

  dd_handle_const(aTHX_ name);

  return o;
}

#endif /* !DD_CONST_VIA_RV2CV */

STATIC void dd_initialize(pTHX) {
  static int initialized = 0;
  if (!initialized) {
    initialized = 1;
#if DD_GROW_VIA_BLOCKHOOK
    {
      static BHK bhk;
#if PERL_VERSION_GE(5,13,6)
      BhkENTRY_set(&bhk, bhk_start, dd_block_start);
#else /* <5.13.6 */
      BhkENTRY_set(&bhk, start, dd_block_start);
#endif /* <5.13.6 */
      Perl_blockhook_register(aTHX_ &bhk);
    }
#else /* !DD_GROW_VIA_BLOCKHOOK */
    hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
#endif /* !DD_GROW_VIA_BLOCKHOOK */
    hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
#if !DD_CONST_VIA_RV2CV
    hook_op_check(OP_CONST, dd_ck_const, NULL);
#endif /* !DD_CONST_VIA_RV2CV */
  }
}

MODULE = Devel::Declare  PACKAGE = Devel::Declare

PROTOTYPES: DISABLE

void
initialize()
  CODE:
    dd_initialize(aTHX);

void
setup()
  CODE:
    dd_initialize(aTHX);
    filter_add(dd_filter_realloc, NULL);

char*
get_linestr()
  CODE:
    RETVAL = dd_get_linestr(aTHX);
  OUTPUT:
    RETVAL

void
set_linestr(char* new_value)
  CODE:
    dd_set_linestr(aTHX_ new_value);

char*
get_lex_stuff()
  CODE:
    RETVAL = dd_get_lex_stuff(aTHX);
  OUTPUT:
    RETVAL

void
clear_lex_stuff()
  CODE:
    dd_clear_lex_stuff(aTHX);

char*
get_curstash_name()
  CODE:
    RETVAL = dd_get_curstash_name(aTHX);
  OUTPUT:
    RETVAL

int
get_linestr_offset()
  CODE:
    RETVAL = dd_get_linestr_offset(aTHX);
  OUTPUT:
    RETVAL

int
toke_scan_word(int offset, int handle_package)
  CODE:
    RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
  OUTPUT:
    RETVAL

int
toke_move_past_token(int offset);
  CODE:
    RETVAL = dd_toke_move_past_token(aTHX_ offset);
  OUTPUT:
    RETVAL

SV*
toke_scan_str(int offset);
  PREINIT:
    int len;
  CODE:
    len = dd_toke_scan_str(aTHX_ offset);
    RETVAL = len ? newSViv(len) : &PL_sv_undef;
  OUTPUT:
    RETVAL

int
toke_scan_ident(int offset)
  CODE:
    RETVAL = dd_toke_scan_ident(aTHX_ offset);
  OUTPUT:
    RETVAL

int
toke_skipspace(int offset)
  CODE:
    RETVAL = dd_toke_skipspace(aTHX_ offset);
  OUTPUT:
    RETVAL

int
get_in_declare()
  CODE:
    RETVAL = in_declare;
  OUTPUT:
    RETVAL

void
set_in_declare(int value)
  CODE:
    in_declare = value;

BOOT:
{
  char *endptr;
  char *debug_str = getenv ("DD_DEBUG");
  if (debug_str) {
    dd_debug = strtol (debug_str, &endptr, 10);
    if (*endptr != '\0') {
      dd_debug = 0;
    }
  }
}