This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c - add optimistic eval (*{ ... }) and (**{ ... })
[perl5.git] / regcomp_internal.h
1 #ifndef REGCOMP_INTERNAL_H
2 #define REGCOMP_INTERNAL_H
3 #ifndef STATIC
4 #define STATIC  static
5 #endif
6 #ifndef RE_OPTIMIZE_CURLYX_TO_CURLYM
7 #define RE_OPTIMIZE_CURLYX_TO_CURLYM 1
8 #endif
9 #ifndef RE_OPTIMIZE_CURLYX_TO_CURLYN
10 #define RE_OPTIMIZE_CURLYX_TO_CURLYN 1
11 #endif
12
13 /* this is a chain of data about sub patterns we are processing that
14    need to be handled separately/specially in study_chunk. Its so
15    we can simulate recursion without losing state.  */
16 struct scan_frame;
17 typedef struct scan_frame {
18     regnode *last_regnode;      /* last node to process in this frame */
19     regnode *next_regnode;      /* next node to process when last is reached */
20     U32 prev_recursed_depth;
21     I32 stopparen;              /* what stopparen do we use */
22     bool in_gosub;              /* this or an outer frame is for GOSUB */
23
24     struct scan_frame *this_prev_frame; /* this previous frame */
25     struct scan_frame *prev_frame;      /* previous frame */
26     struct scan_frame *next_frame;      /* next frame */
27 } scan_frame;
28
29 /* Certain characters are output as a sequence with the first being a
30  * backslash. */
31 #define isBACKSLASHED_PUNCT(c)  memCHRs("-[]\\^", c)
32
33
34 struct RExC_state_t {
35     U32         flags;                  /* RXf_* are we folding, multilining? */
36     U32         pm_flags;               /* PMf_* stuff from the calling PMOP */
37     char        *precomp;               /* uncompiled string. */
38     char        *precomp_end;           /* pointer to end of uncompiled string. */
39     REGEXP      *rx_sv;                 /* The SV that is the regexp. */
40     regexp      *rx;                    /* perl core regexp structure */
41     regexp_internal     *rxi;           /* internal data for regexp object
42                                            pprivate field */
43     char        *start;                 /* Start of input for compile */
44     char        *end;                   /* End of input for compile */
45     char        *parse;                 /* Input-scan pointer. */
46     char        *copy_start;            /* start of copy of input within
47                                            constructed parse string */
48     char        *save_copy_start;       /* Provides one level of saving
49                                            and restoring 'copy_start' */
50     char        *copy_start_in_input;   /* Position in input string
51                                            corresponding to copy_start */
52     SSize_t     whilem_seen;            /* number of WHILEM in this expr */
53     regnode     *emit_start;            /* Start of emitted-code area */
54     regnode_offset emit;                /* Code-emit pointer */
55     I32         naughty;                /* How bad is this pattern? */
56     I32         sawback;                /* Did we see \1, ...? */
57     SSize_t     size;                   /* Number of regnode equivalents in
58                                            pattern */
59     Size_t      sets_depth;              /* Counts recursion depth of already-
60                                            compiled regex set patterns */
61     U32         seen;
62
63     I32      parens_buf_size;           /* #slots malloced open/close_parens */
64     regnode_offset *open_parens;        /* offsets to open parens */
65     regnode_offset *close_parens;       /* offsets to close parens */
66     HV          *paren_names;           /* Paren names */
67
68     /* position beyond 'precomp' of the warning message furthest away from
69      * 'precomp'.  During the parse, no warnings are raised for any problems
70      * earlier in the parse than this position.  This works if warnings are
71      * raised the first time a given spot is parsed, and if only one
72      * independent warning is raised for any given spot */
73     Size_t      latest_warn_offset;
74
75     /* Branch reset /(?|...|...)/ gives us two concepts of capture buffer id.
76      * "Logical Parno" is the user visible view with branch reset taken into
77      * account. "Parno" (or physical parno) is the actual capture buffers in
78      * the pattern *NOT* taking into account branch reset. We also maintain
79      * a map of "next" pointers which allow us to skip to the next physical
80      * capture buffer with the same logical id, with 0 representing "none".
81      *
82      * As we compile we keep track of the two different counts using the
83      * 'logical_npar' and 'npar' members, and we keep track of the upper bound
84      * of both in 'total_par' and 'logical_total_par', we also populate
85      * the 'logical_to_parno' map, which gives us the first physical parno
86      * for a given logical parno, and the `parno_to_logical` array which gives
87      * us the logical id for each physical parno. When compilation is
88      * completed we construct the 'parno_to_logical_next' array from the
89      * 'parno_to_logical' array. (We do not bother constructing it during
90      * compilation as we do not need it, and we can construct it in O(N) time
91      * once we are done, but would need more complicated logic during the
92      * compile, because we want the next pointers to go from smallest to
93      * largest, eg, left to right.)
94      *
95      * Logical: $1      $2  $3  $4    $2  $3    $2    $5
96      * Physical: 1       2   3   4     5   6     7     8
97      * Next:     0       5   6   0     7   0     0     0
98      * Pattern /(a) (?| (b) (c) (d) | (e) (f) | (g) ) (h)/
99      *
100      * As much as possible the internals use and store the physical id of
101      * of capture buffers. We decode the physical to the logical only when
102      * we need to, for instance when someone use $2.
103      *
104      * Note that when branch reset is not used logical and physical are the
105      * same and the next data would be all zero. So when branch reset is not
106      * used we do not need to populate this data into the final regexp.
107      *
108      */
109     I32         *logical_to_parno;        /* logical_parno to parno */
110     I32         *parno_to_logical;        /* parno to logical_parno */
111     I32         *parno_to_logical_next;   /* parno to next (greater value)
112                                              parno with the same
113                                              logical_parno as parno.*/
114
115     I32         npar;                   /* Capture buffer count so far in the
116                                            parse, (OPEN) plus one. ("par" 0 is
117                                            the whole pattern)*/
118     I32         logical_npar;           /* Logical version of npar */
119     I32         total_par;              /* During initial parse, is either 0,
120                                            or -1; the latter indicating a
121                                            reparse is needed.  After that pass,
122                                            it is what 'npar' became after the
123                                            pass.  Hence, it being > 0 indicates
124                                            we are in a reparse situation */
125     I32         logical_total_par;      /* Logical version to total par */
126     I32         nestroot;               /* root parens we are in - used by
127                                            accept */
128     I32         seen_zerolen;
129     regnode     *end_op;                /* END node in program */
130     I32         utf8;           /* whether the pattern is utf8 or not */
131     I32         orig_utf8;      /* whether the pattern was originally in utf8 */
132                                 /* XXX use this for future optimisation of case
133                                  * where pattern must be upgraded to utf8. */
134     I32         uni_semantics;  /* If a d charset modifier should use unicode
135                                    rules, even if the pattern is not in
136                                    utf8 */
137
138     I32         recurse_count;          /* Number of recurse regops we have generated */
139     regnode     **recurse;              /* Recurse regops */
140     U8          *study_chunk_recursed;  /* bitmap of which subs we have moved
141                                            through */
142     U32         study_chunk_recursed_bytes;  /* bytes in bitmap */
143     I32         in_lookaround;
144     I32         contains_locale;
145     I32         override_recoding;
146     I32         recode_x_to_native;
147     I32         in_multi_char_class;
148     int         code_index;             /* next code_blocks[] slot */
149     struct reg_code_blocks *code_blocks;/* positions of literal (?{})
150                                             within pattern */
151     SSize_t     maxlen;                        /* mininum possible number of chars in string to match */
152     scan_frame *frame_head;
153     scan_frame *frame_last;
154     U32         frame_count;
155     AV         *warn_text;
156     HV         *unlexed_names;
157     SV          *runtime_code_qr;       /* qr with the runtime code blocks */
158 #ifdef DEBUGGING
159     const char  *lastparse;
160     I32         lastnum;
161     U32         study_chunk_recursed_count;
162     AV          *paren_name_list;       /* idx -> name */
163     SV          *mysv1;
164     SV          *mysv2;
165
166 #define RExC_lastparse  (pRExC_state->lastparse)
167 #define RExC_lastnum    (pRExC_state->lastnum)
168 #define RExC_paren_name_list    (pRExC_state->paren_name_list)
169 #define RExC_study_chunk_recursed_count    (pRExC_state->study_chunk_recursed_count)
170 #define RExC_mysv       (pRExC_state->mysv1)
171 #define RExC_mysv1      (pRExC_state->mysv1)
172 #define RExC_mysv2      (pRExC_state->mysv2)
173
174 #endif
175     bool        seen_d_op;
176     bool        strict;
177     bool        study_started;
178     bool        in_script_run;
179     bool        use_BRANCHJ;
180     bool        sWARN_EXPERIMENTAL__VLB;
181     bool        sWARN_EXPERIMENTAL__REGEX_SETS;
182 };
183
184 #define RExC_flags      (pRExC_state->flags)
185 #define RExC_pm_flags   (pRExC_state->pm_flags)
186 #define RExC_precomp    (pRExC_state->precomp)
187 #define RExC_copy_start_in_input (pRExC_state->copy_start_in_input)
188 #define RExC_copy_start_in_constructed  (pRExC_state->copy_start)
189 #define RExC_save_copy_start_in_constructed  (pRExC_state->save_copy_start)
190 #define RExC_precomp_end (pRExC_state->precomp_end)
191 #define RExC_rx_sv      (pRExC_state->rx_sv)
192 #define RExC_rx         (pRExC_state->rx)
193 #define RExC_rxi        (pRExC_state->rxi)
194 #define RExC_start      (pRExC_state->start)
195 #define RExC_end        (pRExC_state->end)
196 #define RExC_parse      (pRExC_state->parse)
197 #define RExC_latest_warn_offset (pRExC_state->latest_warn_offset )
198 #define RExC_whilem_seen        (pRExC_state->whilem_seen)
199 #define RExC_seen_d_op (pRExC_state->seen_d_op) /* Seen something that differs
200                                                    under /d from /u ? */
201
202 #define RExC_emit       (pRExC_state->emit)
203 #define RExC_emit_start (pRExC_state->emit_start)
204 #define RExC_sawback    (pRExC_state->sawback)
205 #define RExC_seen       (pRExC_state->seen)
206 #define RExC_size       (pRExC_state->size)
207 #define RExC_maxlen        (pRExC_state->maxlen)
208 #define RExC_logical_npar           (pRExC_state->logical_npar)
209 #define RExC_logical_total_parens   (pRExC_state->logical_total_par)
210 #define RExC_logical_to_parno       (pRExC_state->logical_to_parno)
211 #define RExC_parno_to_logical       (pRExC_state->parno_to_logical)
212 #define RExC_parno_to_logical_next  (pRExC_state->parno_to_logical_next)
213 #define RExC_npar       (pRExC_state->npar)
214 #define RExC_total_parens       (pRExC_state->total_par)
215 #define RExC_parens_buf_size    (pRExC_state->parens_buf_size)
216 #define RExC_nestroot   (pRExC_state->nestroot)
217 #define RExC_seen_zerolen       (pRExC_state->seen_zerolen)
218 #define RExC_utf8       (pRExC_state->utf8)
219 #define RExC_uni_semantics      (pRExC_state->uni_semantics)
220 #define RExC_orig_utf8  (pRExC_state->orig_utf8)
221 #define RExC_open_parens        (pRExC_state->open_parens)
222 #define RExC_close_parens       (pRExC_state->close_parens)
223 #define RExC_end_op     (pRExC_state->end_op)
224 #define RExC_paren_names        (pRExC_state->paren_names)
225 #define RExC_recurse    (pRExC_state->recurse)
226 #define RExC_recurse_count      (pRExC_state->recurse_count)
227 #define RExC_sets_depth         (pRExC_state->sets_depth)
228 #define RExC_study_chunk_recursed        (pRExC_state->study_chunk_recursed)
229 #define RExC_study_chunk_recursed_bytes  \
230                                    (pRExC_state->study_chunk_recursed_bytes)
231 #define RExC_in_lookaround      (pRExC_state->in_lookaround)
232 #define RExC_contains_locale    (pRExC_state->contains_locale)
233 #define RExC_recode_x_to_native (pRExC_state->recode_x_to_native)
234
235 #ifdef EBCDIC
236 #  define SET_recode_x_to_native(x)                                         \
237                     STMT_START { RExC_recode_x_to_native = (x); } STMT_END
238 #else
239 #  define SET_recode_x_to_native(x) NOOP
240 #endif
241
242 #define RExC_in_multi_char_class (pRExC_state->in_multi_char_class)
243 #define RExC_frame_head (pRExC_state->frame_head)
244 #define RExC_frame_last (pRExC_state->frame_last)
245 #define RExC_frame_count (pRExC_state->frame_count)
246 #define RExC_strict (pRExC_state->strict)
247 #define RExC_study_started      (pRExC_state->study_started)
248 #define RExC_warn_text (pRExC_state->warn_text)
249 #define RExC_in_script_run      (pRExC_state->in_script_run)
250 #define RExC_use_BRANCHJ        (pRExC_state->use_BRANCHJ)
251 #define RExC_warned_WARN_EXPERIMENTAL__VLB (pRExC_state->sWARN_EXPERIMENTAL__VLB)
252 #define RExC_warned_WARN_EXPERIMENTAL__REGEX_SETS (pRExC_state->sWARN_EXPERIMENTAL__REGEX_SETS)
253 #define RExC_unlexed_names (pRExC_state->unlexed_names)
254
255
256 /***********************************************************************/
257 /* UTILITY MACROS FOR ADVANCING OR SETTING THE PARSE "CURSOR" RExC_parse
258  *
259  * All of these macros depend on the above RExC_ accessor macros, which
260  * in turns depend on a variable pRExC_state being in scope where they
261  * are used. This is the standard regexp parser context variable which is
262  * passed into every non-trivial parse function in this file.
263  *
264  * Note that the UTF macro is itself a wrapper around RExC_utf8, so all
265  * of the macros which do not take an argument will operate on the
266  * pRExC_state structure *only*.
267  *
268  * Please do NOT modify RExC_parse without using these macros. In the
269  * future these macros will be extended for enhanced debugging and trace
270  * output during the parse process.
271  */
272
273 /* RExC_parse_incf(flag)
274  *
275  * Increment RExC_parse to point at the next codepoint, while doing
276  * the right thing depending on whether we are parsing UTF-8 strings
277  * or not. The 'flag' argument determines if content is UTF-8 or not,
278  * intended for cases where this is NOT governed by the UTF macro.
279  *
280  * Use RExC_parse_inc() if UTF-8ness is controlled by the UTF macro.
281  *
282  * WARNING: Does NOT take into account RExC_end; it is the callers
283  * responsibility to make sure there are enough octets left in
284  * RExC_parse to ensure that when processing UTF-8 we would not read
285  * past the end of the string.
286  */
287 #define RExC_parse_incf(flag) STMT_START {              \
288     RExC_parse += (flag) ? UTF8SKIP(RExC_parse) : 1;    \
289 } STMT_END
290
291 /* RExC_parse_inc_safef(flag)
292  *
293  * Safely increment RExC_parse to point at the next codepoint,
294  * doing the right thing depending on whether we are parsing
295  * UTF-8 strings or not and NOT reading past the end of the buffer.
296  * The 'flag' argument determines if content is UTF-8 or not,
297  * intended for cases where this is NOT governed by the UTF macro.
298  *
299  * Use RExC_parse_safe() if UTF-8ness is controlled by the UTF macro.
300  *
301  * NOTE: Will NOT read past RExC_end when content is UTF-8.
302  */
303 #define RExC_parse_inc_safef(flag) STMT_START {                     \
304     RExC_parse += (flag) ? UTF8_SAFE_SKIP(RExC_parse,RExC_end) : 1; \
305 } STMT_END
306
307 /* RExC_parse_inc()
308  *
309  * Increment RExC_parse to point at the next codepoint,
310  * doing the right thing depending on whether we are parsing
311  * UTF-8 strings or not.
312  *
313  * WARNING: Does NOT take into account RExC_end, it is the callers
314  * responsibility to make sure there are enough octets left in
315  * RExC_parse to ensure that when processing UTF-8 we would not read
316  * past the end of the string.
317  *
318  * NOTE: whether we are parsing UTF-8 or not is determined by the
319  * UTF macro which is defined as cBOOL(RExC_parse_utf8), thus this
320  * macro operates on the pRExC_state structure only.
321  */
322 #define RExC_parse_inc() RExC_parse_incf(UTF)
323
324 /* RExC_parse_inc_safe()
325  *
326  * Safely increment RExC_parse to point at the next codepoint,
327  * doing the right thing depending on whether we are parsing
328  * UTF-8 strings or not and NOT reading past the end of the buffer.
329  *
330  * NOTE: whether we are parsing UTF-8 or not is determined by the
331  * UTF macro which is defined as cBOOL(RExC_parse_utf8), thus this
332  * macro operates on the pRExC_state structure only.
333  */
334 #define RExC_parse_inc_safe() RExC_parse_inc_safef(UTF)
335
336 /* RExC_parse_inc_utf8()
337  *
338  * Increment RExC_parse to point at the next utf8 codepoint,
339  * assumes content is UTF-8.
340  *
341  * WARNING: Does NOT take into account RExC_end; it is the callers
342  * responsibility to make sure there are enough octets left in RExC_parse
343  * to ensure that when processing UTF-8 we would not read past the end
344  * of the string.
345  */
346 #define RExC_parse_inc_utf8() STMT_START {  \
347     RExC_parse += UTF8SKIP(RExC_parse);     \
348 } STMT_END
349
350 /* RExC_parse_inc_if_char()
351  *
352  * Increment RExC_parse to point at the next codepoint, if and only
353  * if the current parse point is NOT a NULL, while doing the right thing
354  * depending on whether we are parsing UTF-8 strings or not.
355  *
356  * WARNING: Does NOT take into account RExC_end, it is the callers
357  * responsibility to make sure there are enough octets left in RExC_parse
358  * to ensure that when processing UTF-8 we would not read past the end
359  * of the string.
360  *
361  * NOTE: whether we are parsing UTF-8 or not is determined by the
362  * UTF macro which is defined as cBOOL(RExC_parse_utf8), thus this
363  * macro operates on the pRExC_state structure only.
364  */
365 #define RExC_parse_inc_if_char() STMT_START {         \
366     RExC_parse += SKIP_IF_CHAR(RExC_parse,RExC_end);  \
367 } STMT_END
368
369 /* RExC_parse_inc_by(n_octets)
370  *
371  * Increment the parse cursor by the number of octets specified by
372  * the 'n_octets' argument.
373  *
374  * NOTE: Does NOT check ANY constraints. It is the callers responsibility
375  * that this will not move past the end of the string, or leave the
376  * pointer in the middle of a UTF-8 sequence.
377  *
378  * Typically used to advanced past previously analyzed content.
379  */
380 #define RExC_parse_inc_by(n_octets) STMT_START {  \
381     RExC_parse += (n_octets);                     \
382 } STMT_END
383
384 /* RExC_parse_set(to_ptr)
385  *
386  * Sets the RExC_parse pointer to the pointer specified by the 'to'
387  * argument. No validation whatsoever is performed on the to pointer.
388  */
389 #define RExC_parse_set(to_ptr) STMT_START { \
390     RExC_parse = (to_ptr);                  \
391 } STMT_END
392
393 /**********************************************************************/
394
395 /* Heuristic check on the complexity of the pattern: if TOO_NAUGHTY, we set
396  * a flag to disable back-off on the fixed/floating substrings - if it's
397  * a high complexity pattern we assume the benefit of avoiding a full match
398  * is worth the cost of checking for the substrings even if they rarely help.
399  */
400 #define RExC_naughty    (pRExC_state->naughty)
401 #define TOO_NAUGHTY (10)
402 #define MARK_NAUGHTY(add) \
403     if (RExC_naughty < TOO_NAUGHTY) \
404         RExC_naughty += (add)
405 #define MARK_NAUGHTY_EXP(exp, add) \
406     if (RExC_naughty < TOO_NAUGHTY) \
407         RExC_naughty += RExC_naughty / (exp) + (add)
408
409 #define isNON_BRACE_QUANTIFIER(c)   ((c) == '*' || (c) == '+' || (c) == '?')
410 #define isQUANTIFIER(s,e)  (   isNON_BRACE_QUANTIFIER(*s)                      \
411                             || ((*s) == '{' && regcurly(s, e, NULL)))
412
413 /*
414  * Flags to be passed up.
415  */
416 #define HASWIDTH        0x01    /* Known to not match null strings, could match
417                                    non-null ones. */
418 #define SIMPLE          0x02    /* Exactly one character wide */
419                                 /* (or LNBREAK as a special case) */
420 #define POSTPONED       0x08    /* (?1),(?&name), (??{...}) or similar */
421 #define TRYAGAIN        0x10    /* Weeded out a declaration. */
422 #define RESTART_PARSE   0x20    /* Need to redo the parse */
423 #define NEED_UTF8       0x40    /* In conjunction with RESTART_PARSE, need to
424                                    calcuate sizes as UTF-8 */
425
426 #define REG_NODE_NUM(x) ((x) ? (int)((x)-RExC_emit_start) : -1)
427
428 /* whether trie related optimizations are enabled */
429 #if PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION
430 #define TRIE_STUDY_OPT
431 #define FULL_TRIE_STUDY
432 #define TRIE_STCLASS
433 #endif
434
435 /* About the term "restudy" and the var "restudied" and the defines
436  * "SCF_TRIE_RESTUDY" and "SCF_TRIE_DOING_RESTUDY": All of these relate to
437  * doing multiple study_chunk() calls over the same set of opcodes for* the
438  * purpose of enhanced TRIE optimizations.
439  *
440  * Specifically, when TRIE_STUDY_OPT is defined, and it is defined in normal
441  * builds, (see above), during compilation SCF_TRIE_RESTUDY may be enabled
442  * which then causes the Perl_re_op_compile() to then call the optimizer
443  * S_study_chunk() a second time to perform additional optimizations,
444  * including the aho_corasick startclass optimization.
445  * This additional pass will only happen once, which is managed by the
446  * 'restudied' variable in Perl_re_op_compile().
447  *
448  * When this second pass is under way the flags passed into study_chunk() will
449  * include SCF_TRIE_DOING_RESTUDY and this flag is and must be cascaded down
450  * to any recursive calls to S_study_chunk().
451  *
452  * IMPORTANT: Any logic in study_chunk() that emits warnings should check that
453  * the SCF_TRIE_DOING_RESTUDY flag is NOT set in 'flags', or the warning may
454  * be produced twice.
455  *
456  * See commit 07be1b83a6b2d24b492356181ddf70e1c7917ae3 and
457  * 688e03912e3bff2d2419c457d8b0e1bab3eb7112 for more details.
458  */
459
460
461 #define PBYTE(u8str,paren) ((U8*)(u8str))[(paren) >> 3]
462 #define PBITVAL(paren) (1 << ((paren) & 7))
463 #define PAREN_OFFSET(depth) \
464     (RExC_study_chunk_recursed + (depth) * RExC_study_chunk_recursed_bytes)
465 #define PAREN_TEST(depth, paren) \
466     (PBYTE(PAREN_OFFSET(depth), paren) & PBITVAL(paren))
467 #define PAREN_SET(depth, paren) \
468     (PBYTE(PAREN_OFFSET(depth), paren) |= PBITVAL(paren))
469 #define PAREN_UNSET(depth, paren) \
470     (PBYTE(PAREN_OFFSET(depth), paren) &= ~PBITVAL(paren))
471
472 #define REQUIRE_UTF8(flagp) STMT_START {                                   \
473                                      if (!UTF) {                           \
474                                          *flagp = RESTART_PARSE|NEED_UTF8; \
475                                          return 0;                         \
476                                      }                                     \
477                              } STMT_END
478
479 /* /u is to be chosen if we are supposed to use Unicode rules, or if the
480  * pattern is in UTF-8.  This latter condition is in case the outermost rules
481  * are locale.  See GH #17278 */
482 #define toUSE_UNI_CHARSET_NOT_DEPENDS (RExC_uni_semantics || UTF)
483
484 /* Change from /d into /u rules, and restart the parse.  RExC_uni_semantics is
485  * a flag that indicates we need to override /d with /u as a result of
486  * something in the pattern.  It should only be used in regards to calling
487  * set_regex_charset() or get_regex_charset() */
488 #define REQUIRE_UNI_RULES(flagp, restart_retval)                            \
489     STMT_START {                                                            \
490             if (DEPENDS_SEMANTICS) {                                        \
491                 set_regex_charset(&RExC_flags, REGEX_UNICODE_CHARSET);      \
492                 RExC_uni_semantics = 1;                                     \
493                 if (RExC_seen_d_op && LIKELY(! IN_PARENS_PASS)) {           \
494                     /* No need to restart the parse if we haven't seen      \
495                      * anything that differs between /u and /d, and no need \
496                      * to restart immediately if we're going to reparse     \
497                      * anyway to count parens */                            \
498                     *flagp |= RESTART_PARSE;                                \
499                     return restart_retval;                                  \
500                 }                                                           \
501             }                                                               \
502     } STMT_END
503
504 #define REQUIRE_BRANCHJ(flagp, restart_retval)                              \
505     STMT_START {                                                            \
506                 RExC_use_BRANCHJ = 1;                                       \
507                 *flagp |= RESTART_PARSE;                                    \
508                 return restart_retval;                                      \
509     } STMT_END
510
511 /* Until we have completed the parse, we leave RExC_total_parens at 0 or
512  * less.  After that, it must always be positive, because the whole re is
513  * considered to be surrounded by virtual parens.  Setting it to negative
514  * indicates there is some construct that needs to know the actual number of
515  * parens to be properly handled.  And that means an extra pass will be
516  * required after we've counted them all */
517 #define ALL_PARENS_COUNTED (RExC_total_parens > 0)
518 #define REQUIRE_PARENS_PASS                                                 \
519     STMT_START {  /* No-op if have completed a pass */                      \
520                     if (! ALL_PARENS_COUNTED) RExC_total_parens = -1;       \
521     } STMT_END
522 #define IN_PARENS_PASS (RExC_total_parens < 0)
523
524
525 /* This is used to return failure (zero) early from the calling function if
526  * various flags in 'flags' are set.  Two flags always cause a return:
527  * 'RESTART_PARSE' and 'NEED_UTF8'.   'extra' can be used to specify any
528  * additional flags that should cause a return; 0 if none.  If the return will
529  * be done, '*flagp' is first set to be all of the flags that caused the
530  * return. */
531 #define RETURN_FAIL_ON_RESTART_OR_FLAGS(flags,flagp,extra)                  \
532     STMT_START {                                                            \
533             if ((flags) & (RESTART_PARSE|NEED_UTF8|(extra))) {              \
534                 *(flagp) = (flags) & (RESTART_PARSE|NEED_UTF8|(extra));     \
535                 return 0;                                                   \
536             }                                                               \
537     } STMT_END
538
539 #define MUST_RESTART(flags) ((flags) & (RESTART_PARSE))
540
541 #define RETURN_FAIL_ON_RESTART(flags,flagp)                                 \
542                         RETURN_FAIL_ON_RESTART_OR_FLAGS( flags, flagp, 0)
543 #define RETURN_FAIL_ON_RESTART_FLAGP(flagp)                                 \
544                                     if (MUST_RESTART(*(flagp))) return 0
545
546 /* This converts the named class defined in regcomp.h to its equivalent class
547  * number defined in handy.h. */
548 #define namedclass_to_classnum(class)  ((int) ((class) / 2))
549 #define classnum_to_namedclass(classnum)  ((classnum) * 2)
550
551 #define _invlist_union_complement_2nd(a, b, output) \
552                         _invlist_union_maybe_complement_2nd(a, b, TRUE, output)
553 #define _invlist_intersection_complement_2nd(a, b, output) \
554                  _invlist_intersection_maybe_complement_2nd(a, b, TRUE, output)
555
556 /* We add a marker if we are deferring expansion of a property that is both
557  * 1) potentiallly user-defined; and
558  * 2) could also be an official Unicode property.
559  *
560  * Without this marker, any deferred expansion can only be for a user-defined
561  * one.  This marker shouldn't conflict with any that could be in a legal name,
562  * and is appended to its name to indicate this.  There is a string and
563  * character form */
564 #define DEFERRED_COULD_BE_OFFICIAL_MARKERs  "~"
565 #define DEFERRED_COULD_BE_OFFICIAL_MARKERc  '~'
566
567 /* What is infinity for optimization purposes */
568 #define OPTIMIZE_INFTY  SSize_t_MAX
569
570 /* About scan_data_t.
571
572   During optimisation we recurse through the regexp program performing
573   various inplace (keyhole style) optimisations. In addition study_chunk
574   and scan_commit populate this data structure with information about
575   what strings MUST appear in the pattern. We look for the longest
576   string that must appear at a fixed location, and we look for the
577   longest string that may appear at a floating location. So for instance
578   in the pattern:
579
580     /FOO[xX]A.*B[xX]BAR/
581
582   Both 'FOO' and 'A' are fixed strings. Both 'B' and 'BAR' are floating
583   strings (because they follow a .* construct). study_chunk will identify
584   both FOO and BAR as being the longest fixed and floating strings respectively.
585
586   The strings can be composites, for instance
587
588      /(f)(o)(o)/
589
590   will result in a composite fixed substring 'foo'.
591
592   For each string some basic information is maintained:
593
594   - min_offset
595     This is the position the string must appear at, or not before.
596     It also implicitly (when combined with minlenp) tells us how many
597     characters must match before the string we are searching for.
598     Likewise when combined with minlenp and the length of the string it
599     tells us how many characters must appear after the string we have
600     found.
601
602   - max_offset
603     Only used for floating strings. This is the rightmost point that
604     the string can appear at. If set to OPTIMIZE_INFTY it indicates that the
605     string can occur infinitely far to the right.
606     For fixed strings, it is equal to min_offset.
607
608   - minlenp
609     A pointer to the minimum number of characters of the pattern that the
610     string was found inside. This is important as in the case of positive
611     lookahead or positive lookbehind we can have multiple patterns
612     involved. Consider
613
614     /(?=FOO).*F/
615
616     The minimum length of the pattern overall is 3, the minimum length
617     of the lookahead part is 3, but the minimum length of the part that
618     will actually match is 1. So 'FOO's minimum length is 3, but the
619     minimum length for the F is 1. This is important as the minimum length
620     is used to determine offsets in front of and behind the string being
621     looked for.  Since strings can be composites this is the length of the
622     pattern at the time it was committed with a scan_commit. Note that
623     the length is calculated by study_chunk, so that the minimum lengths
624     are not known until the full pattern has been compiled, thus the
625     pointer to the value.
626
627   - lookbehind
628
629     In the case of lookbehind the string being searched for can be
630     offset past the start point of the final matching string.
631     If this value was just blithely removed from the min_offset it would
632     invalidate some of the calculations for how many chars must match
633     before or after (as they are derived from min_offset and minlen and
634     the length of the string being searched for).
635     When the final pattern is compiled and the data is moved from the
636     scan_data_t structure into the regexp structure the information
637     about lookbehind is factored in, with the information that would
638     have been lost precalculated in the end_shift field for the
639     associated string.
640
641   The fields pos_min and pos_delta are used to store the minimum offset
642   and the delta to the maximum offset at the current point in the pattern.
643
644 */
645
646 struct scan_data_substrs {
647     SV      *str;       /* longest substring found in pattern */
648     SSize_t min_offset; /* earliest point in string it can appear */
649     SSize_t max_offset; /* latest point in string it can appear */
650     SSize_t *minlenp;   /* pointer to the minlen relevant to the string */
651     SSize_t lookbehind; /* is the pos of the string modified by LB */
652     I32 flags;          /* per substring SF_* and SCF_* flags */
653 };
654
655 /* this is typedef'ed in perl.h */
656 struct scan_data_t {
657     /*I32 len_min;      unused */
658     /*I32 len_delta;    unused */
659     SSize_t pos_min;
660     SSize_t pos_delta;
661     SV *last_found;
662     SSize_t last_end;       /* min value, <0 unless valid. */
663     SSize_t last_start_min;
664     SSize_t last_start_max;
665     U8      cur_is_floating; /* whether the last_* values should be set as
666                               * the next fixed (0) or floating (1)
667                               * substring */
668
669     /* [0] is longest fixed substring so far, [1] is longest float so far */
670     struct scan_data_substrs  substrs[2];
671
672     I32 flags;             /* common SF_* and SCF_* flags */
673     I32 whilem_c;
674     SSize_t *last_closep;
675     regnode **last_close_opp; /* pointer to pointer to last CLOSE regop
676                                  seen. DO NOT DEREFERENCE the regnode
677                                  pointer - the op may have been optimized
678                                  away */
679     regnode_ssc *start_class;
680 };
681
682 /*
683  * Forward declarations for pregcomp()'s friends.
684  */
685
686 static const scan_data_t zero_scan_data = {
687     0, 0, NULL, 0, 0, 0, 0,
688     {
689         { NULL, 0, 0, 0, 0, 0 },
690         { NULL, 0, 0, 0, 0, 0 },
691     },
692     0, 0, NULL, NULL, NULL
693 };
694
695 /* study flags */
696
697 #define SF_BEFORE_SEOL          0x0001
698 #define SF_BEFORE_MEOL          0x0002
699 #define SF_BEFORE_EOL           (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
700
701 #define SF_IS_INF               0x0040
702 #define SF_HAS_PAR              0x0080
703 #define SF_IN_PAR               0x0100
704 #define SF_HAS_EVAL             0x0200
705
706
707 /* SCF_DO_SUBSTR is the flag that tells the regexp analyzer to track the
708  * longest substring in the pattern. When it is not set the optimiser keeps
709  * track of position, but does not keep track of the actual strings seen,
710  *
711  * So for instance /foo/ will be parsed with SCF_DO_SUBSTR being true, but
712  * /foo/i will not.
713  *
714  * Similarly, /foo.*(blah|erm|huh).*fnorble/ will have "foo" and "fnorble"
715  * parsed with SCF_DO_SUBSTR on, but while processing the (...) it will be
716  * turned off because of the alternation (BRANCH). */
717 #define SCF_DO_SUBSTR           0x0400
718
719 #define SCF_DO_STCLASS_AND      0x0800
720 #define SCF_DO_STCLASS_OR       0x1000
721 #define SCF_DO_STCLASS          (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
722 #define SCF_WHILEM_VISITED_POS  0x2000
723
724 #define SCF_TRIE_RESTUDY        0x4000 /* Need to do restudy in study_chunk()?
725                                           Search for "restudy" in this file
726                                           to find a detailed explanation.*/
727 #define SCF_SEEN_ACCEPT         0x8000
728 #define SCF_TRIE_DOING_RESTUDY 0x10000 /* Are we in restudy right now?
729                                           Search for "restudy" in this file
730                                           to find a detailed explanation. */
731 #define SCF_IN_DEFINE          0x20000
732
733
734
735 #define UTF cBOOL(RExC_utf8)
736
737 /* The enums for all these are ordered so things work out correctly */
738 #define LOC (get_regex_charset(RExC_flags) == REGEX_LOCALE_CHARSET)
739 #define DEPENDS_SEMANTICS (get_regex_charset(RExC_flags)                    \
740                                                      == REGEX_DEPENDS_CHARSET)
741 #define UNI_SEMANTICS (get_regex_charset(RExC_flags) == REGEX_UNICODE_CHARSET)
742 #define AT_LEAST_UNI_SEMANTICS (get_regex_charset(RExC_flags)                \
743                                                      >= REGEX_UNICODE_CHARSET)
744 #define ASCII_RESTRICTED (get_regex_charset(RExC_flags)                      \
745                                             == REGEX_ASCII_RESTRICTED_CHARSET)
746 #define AT_LEAST_ASCII_RESTRICTED (get_regex_charset(RExC_flags)             \
747                                             >= REGEX_ASCII_RESTRICTED_CHARSET)
748 #define ASCII_FOLD_RESTRICTED (get_regex_charset(RExC_flags)                 \
749                                         == REGEX_ASCII_MORE_RESTRICTED_CHARSET)
750
751 #define FOLD cBOOL(RExC_flags & RXf_PMf_FOLD)
752
753 /* For programs that want to be strictly Unicode compatible by dying if any
754  * attempt is made to match a non-Unicode code point against a Unicode
755  * property.  */
756 #define ALWAYS_WARN_SUPER  ckDEAD(packWARN(WARN_NON_UNICODE))
757
758 #define OOB_NAMEDCLASS          -1
759
760 /* There is no code point that is out-of-bounds, so this is problematic.  But
761  * its only current use is to initialize a variable that is always set before
762  * looked at. */
763 #define OOB_UNICODE             0xDEADBEEF
764
765 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
766
767
768 /* length of regex to show in messages that don't mark a position within */
769 #define RegexLengthToShowInErrorMessages 127
770
771 /*
772  * If MARKER[12] are adjusted, be sure to adjust the constants at the top
773  * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
774  * op/pragma/warn/regcomp.
775  */
776 #define MARKER1 "<-- HERE"    /* marker as it appears in the description */
777 #define MARKER2 " <-- HERE "  /* marker as it appears within the regex */
778
779 #define REPORT_LOCATION " in regex; marked by " MARKER1    \
780                         " in m/%" UTF8f MARKER2 "%" UTF8f "/"
781
782 /* The code in this file in places uses one level of recursion with parsing
783  * rebased to an alternate string constructed by us in memory.  This can take
784  * the form of something that is completely different from the input, or
785  * something that uses the input as part of the alternate.  In the first case,
786  * there should be no possibility of an error, as we are in complete control of
787  * the alternate string.  But in the second case we don't completely control
788  * the input portion, so there may be errors in that.  Here's an example:
789  *      /[abc\x{DF}def]/ui
790  * is handled specially because \x{df} folds to a sequence of more than one
791  * character: 'ss'.  What is done is to create and parse an alternate string,
792  * which looks like this:
793  *      /(?:\x{DF}|[abc\x{DF}def])/ui
794  * where it uses the input unchanged in the middle of something it constructs,
795  * which is a branch for the DF outside the character class, and clustering
796  * parens around the whole thing. (It knows enough to skip the DF inside the
797  * class while in this substitute parse.) 'abc' and 'def' may have errors that
798  * need to be reported.  The general situation looks like this:
799  *
800  *                                       |<------- identical ------>|
801  *              sI                       tI               xI       eI
802  * Input:       ---------------------------------------------------------------
803  * Constructed:         ---------------------------------------------------
804  *                      sC               tC               xC       eC     EC
805  *                                       |<------- identical ------>|
806  *
807  * sI..eI   is the portion of the input pattern we are concerned with here.
808  * sC..EC   is the constructed substitute parse string.
809  *  sC..tC  is constructed by us
810  *  tC..eC  is an exact duplicate of the portion of the input pattern tI..eI.
811  *          In the diagram, these are vertically aligned.
812  *  eC..EC  is also constructed by us.
813  * xC       is the position in the substitute parse string where we found a
814  *          problem.
815  * xI       is the position in the original pattern corresponding to xC.
816  *
817  * We want to display a message showing the real input string.  Thus we need to
818  * translate from xC to xI.  We know that xC >= tC, since the portion of the
819  * string sC..tC has been constructed by us, and so shouldn't have errors.  We
820  * get:
821  *      xI = tI + (xC - tC)
822  *
823  * When the substitute parse is constructed, the code needs to set:
824  *      RExC_start (sC)
825  *      RExC_end (eC)
826  *      RExC_copy_start_in_input  (tI)
827  *      RExC_copy_start_in_constructed (tC)
828  * and restore them when done.
829  *
830  * During normal processing of the input pattern, both
831  * 'RExC_copy_start_in_input' and 'RExC_copy_start_in_constructed' are set to
832  * sI, so that xC equals xI.
833  */
834
835 #define sI              RExC_precomp
836 #define eI              RExC_precomp_end
837 #define sC              RExC_start
838 #define eC              RExC_end
839 #define tI              RExC_copy_start_in_input
840 #define tC              RExC_copy_start_in_constructed
841 #define xI(xC)          (tI + (xC - tC))
842 #define xI_offset(xC)   (xI(xC) - sI)
843
844 #define REPORT_LOCATION_ARGS(xC)                                            \
845     UTF8fARG(UTF,                                                           \
846              (xI(xC) > eI) /* Don't run off end */                          \
847               ? eI - sI   /* Length before the <--HERE */                   \
848               : ((xI_offset(xC) >= 0)                                       \
849                  ? xI_offset(xC)                                            \
850                  : (Perl_croak(aTHX_ "panic: %s: %d: negative offset: %"    \
851                                     IVdf " trying to output message for "   \
852                                     " pattern %.*s",                        \
853                                     __FILE__, __LINE__, (IV) xI_offset(xC), \
854                                     ((int) (eC - sC)), sC), 0)),            \
855              sI),         /* The input pattern printed up to the <--HERE */ \
856     UTF8fARG(UTF,                                                           \
857              (xI(xC) > eI) ? 0 : eI - xI(xC), /* Length after <--HERE */    \
858              (xI(xC) > eI) ? eI : xI(xC))     /* pattern after <--HERE */
859
860 /* Used to point after bad bytes for an error message, but avoid skipping
861  * past a nul byte. */
862 #define SKIP_IF_CHAR(s, e) (!*(s) ? 0 : UTF ? UTF8_SAFE_SKIP(s, e) : 1)
863
864 /* Set up to clean up after our imminent demise */
865 #define PREPARE_TO_DIE                                                      \
866     STMT_START {                                                            \
867         if (RExC_rx_sv)                                                     \
868             SAVEFREESV(RExC_rx_sv);                                         \
869         if (RExC_open_parens)                                               \
870             SAVEFREEPV(RExC_open_parens);                                   \
871         if (RExC_close_parens)                                              \
872             SAVEFREEPV(RExC_close_parens);                                  \
873         if (RExC_logical_to_parno)                                          \
874             SAVEFREEPV(RExC_logical_to_parno);                              \
875         if (RExC_parno_to_logical)                                          \
876             SAVEFREEPV(RExC_parno_to_logical);                              \
877     } STMT_END
878
879 /*
880  * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
881  * arg. Show regex, up to a maximum length. If it's too long, chop and add
882  * "...".
883  */
884 #define _FAIL(code) STMT_START {                                        \
885     const char *ellipses = "";                                          \
886     IV len = RExC_precomp_end - RExC_precomp;                           \
887                                                                         \
888     PREPARE_TO_DIE;                                                     \
889     if (len > RegexLengthToShowInErrorMessages) {                       \
890         /* chop 10 shorter than the max, to ensure meaning of "..." */  \
891         len = RegexLengthToShowInErrorMessages - 10;                    \
892         ellipses = "...";                                               \
893     }                                                                   \
894     code;                                                               \
895 } STMT_END
896
897 #define FAIL(msg) _FAIL(                            \
898     Perl_croak(aTHX_ "%s in regex m/%" UTF8f "%s/",         \
899             msg, UTF8fARG(UTF, len, RExC_precomp), ellipses))
900
901 #define FAIL2(msg,arg) _FAIL(                       \
902     Perl_croak(aTHX_ msg " in regex m/%" UTF8f "%s/",       \
903             arg, UTF8fARG(UTF, len, RExC_precomp), ellipses))
904
905 #define FAIL3(msg,arg1,arg2) _FAIL(                         \
906     Perl_croak(aTHX_ msg " in regex m/%" UTF8f "%s/",       \
907      arg1, arg2, UTF8fARG(UTF, len, RExC_precomp), ellipses))
908
909 /*
910  * Simple_vFAIL -- like FAIL, but marks the current location in the scan
911  */
912 #define Simple_vFAIL(m) STMT_START {                                    \
913     Perl_croak(aTHX_ "%s" REPORT_LOCATION,                              \
914             m, REPORT_LOCATION_ARGS(RExC_parse));                       \
915 } STMT_END
916
917 /*
918  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
919  */
920 #define vFAIL(m) STMT_START {                           \
921     PREPARE_TO_DIE;                                     \
922     Simple_vFAIL(m);                                    \
923 } STMT_END
924
925 /*
926  * Like Simple_vFAIL(), but accepts two arguments.
927  */
928 #define Simple_vFAIL2(m,a1) STMT_START {                        \
929     S_re_croak(aTHX_ UTF, m REPORT_LOCATION, a1,                \
930                       REPORT_LOCATION_ARGS(RExC_parse));        \
931 } STMT_END
932
933 /*
934  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
935  */
936 #define vFAIL2(m,a1) STMT_START {                       \
937     PREPARE_TO_DIE;                                     \
938     Simple_vFAIL2(m, a1);                               \
939 } STMT_END
940
941
942 /*
943  * Like Simple_vFAIL(), but accepts three arguments.
944  */
945 #define Simple_vFAIL3(m, a1, a2) STMT_START {                   \
946     S_re_croak(aTHX_ UTF, m REPORT_LOCATION, a1, a2,            \
947             REPORT_LOCATION_ARGS(RExC_parse));                  \
948 } STMT_END
949
950 /*
951  * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
952  */
953 #define vFAIL3(m,a1,a2) STMT_START {                    \
954     PREPARE_TO_DIE;                                     \
955     Simple_vFAIL3(m, a1, a2);                           \
956 } STMT_END
957
958 /*
959  * Like Simple_vFAIL(), but accepts four arguments.
960  */
961 #define Simple_vFAIL4(m, a1, a2, a3) STMT_START {               \
962     S_re_croak(aTHX_ UTF, m REPORT_LOCATION, a1, a2, a3,        \
963             REPORT_LOCATION_ARGS(RExC_parse));                  \
964 } STMT_END
965
966 #define vFAIL4(m,a1,a2,a3) STMT_START {                 \
967     PREPARE_TO_DIE;                                     \
968     Simple_vFAIL4(m, a1, a2, a3);                       \
969 } STMT_END
970
971 /* A specialized version of vFAIL2 that works with UTF8f */
972 #define vFAIL2utf8f(m, a1) STMT_START {             \
973     PREPARE_TO_DIE;                                 \
974     S_re_croak(aTHX_ UTF, m REPORT_LOCATION, a1,  \
975             REPORT_LOCATION_ARGS(RExC_parse));      \
976 } STMT_END
977
978 #define vFAIL3utf8f(m, a1, a2) STMT_START {             \
979     PREPARE_TO_DIE;                                     \
980     S_re_croak(aTHX_ UTF, m REPORT_LOCATION, a1, a2,  \
981             REPORT_LOCATION_ARGS(RExC_parse));          \
982 } STMT_END
983
984 /* Setting this to NULL is a signal to not output warnings */
985 #define TURN_OFF_WARNINGS_IN_SUBSTITUTE_PARSE                               \
986     STMT_START {                                                            \
987       RExC_save_copy_start_in_constructed  = RExC_copy_start_in_constructed;\
988       RExC_copy_start_in_constructed = NULL;                                \
989     } STMT_END
990 #define RESTORE_WARNINGS                                                    \
991     RExC_copy_start_in_constructed = RExC_save_copy_start_in_constructed
992
993 /* Since a warning can be generated multiple times as the input is reparsed, we
994  * output it the first time we come to that point in the parse, but suppress it
995  * otherwise.  'RExC_copy_start_in_constructed' being NULL is a flag to not
996  * generate any warnings */
997 #define TO_OUTPUT_WARNINGS(loc)                                         \
998   (   RExC_copy_start_in_constructed                                    \
999    && ((xI(loc)) - RExC_precomp) > (Ptrdiff_t) RExC_latest_warn_offset)
1000
1001 /* After we've emitted a warning, we save the position in the input so we don't
1002  * output it again */
1003 #define UPDATE_WARNINGS_LOC(loc)                                        \
1004     STMT_START {                                                        \
1005         if (TO_OUTPUT_WARNINGS(loc)) {                                  \
1006             RExC_latest_warn_offset = MAX(sI, MIN(eI, xI(loc)))         \
1007                                                        - RExC_precomp;  \
1008         }                                                               \
1009     } STMT_END
1010
1011 /* 'warns' is the output of the packWARNx macro used in 'code' */
1012 #define _WARN_HELPER(loc, warns, code)                                  \
1013     STMT_START {                                                        \
1014         if (! RExC_copy_start_in_constructed) {                         \
1015             Perl_croak( aTHX_ "panic! %s: %d: Tried to warn when none"  \
1016                               " expected at '%s'",                      \
1017                               __FILE__, __LINE__, loc);                 \
1018         }                                                               \
1019         if (TO_OUTPUT_WARNINGS(loc)) {                                  \
1020             if (ckDEAD(warns))                                          \
1021                 PREPARE_TO_DIE;                                         \
1022             code;                                                       \
1023             UPDATE_WARNINGS_LOC(loc);                                   \
1024         }                                                               \
1025     } STMT_END
1026
1027 /* m is not necessarily a "literal string", in this macro */
1028 #define warn_non_literal_string(loc, packed_warn, m)                    \
1029     _WARN_HELPER(loc, packed_warn,                                      \
1030                       Perl_warner(aTHX_ packed_warn,                    \
1031                                        "%s" REPORT_LOCATION,            \
1032                                   m, REPORT_LOCATION_ARGS(loc)))
1033 #define reg_warn_non_literal_string(loc, m)                             \
1034                 warn_non_literal_string(loc, packWARN(WARN_REGEXP), m)
1035
1036 #define ckWARN2_non_literal_string(loc, packwarn, m, a1)                    \
1037     STMT_START {                                                            \
1038                 char * format;                                              \
1039                 Size_t format_size = strlen(m) + strlen(REPORT_LOCATION)+ 1;\
1040                 Newx(format, format_size, char);                            \
1041                 my_strlcpy(format, m, format_size);                         \
1042                 my_strlcat(format, REPORT_LOCATION, format_size);           \
1043                 SAVEFREEPV(format);                                         \
1044                 _WARN_HELPER(loc, packwarn,                                 \
1045                       Perl_ck_warner(aTHX_ packwarn,                        \
1046                                         format,                             \
1047                                         a1, REPORT_LOCATION_ARGS(loc)));    \
1048     } STMT_END
1049
1050 #define ckWARNreg(loc,m)                                                \
1051     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                            \
1052                       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),       \
1053                                           m REPORT_LOCATION,            \
1054                                           REPORT_LOCATION_ARGS(loc)))
1055
1056 #define vWARN(loc, m)                                                   \
1057     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                            \
1058                       Perl_warner(aTHX_ packWARN(WARN_REGEXP),          \
1059                                        m REPORT_LOCATION,               \
1060                                        REPORT_LOCATION_ARGS(loc)))      \
1061
1062 #define vWARN_dep(loc, m)                                               \
1063     _WARN_HELPER(loc, packWARN(WARN_DEPRECATED),                        \
1064                       Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),      \
1065                                        m REPORT_LOCATION,               \
1066                                        REPORT_LOCATION_ARGS(loc)))
1067
1068 #define ckWARNdep(loc,m)                                                \
1069     _WARN_HELPER(loc, packWARN(WARN_DEPRECATED),                        \
1070                       Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), \
1071                                             m REPORT_LOCATION,          \
1072                                             REPORT_LOCATION_ARGS(loc)))
1073
1074 #define ckWARNregdep(loc,m)                                                 \
1075     _WARN_HELPER(loc, packWARN2(WARN_DEPRECATED, WARN_REGEXP),              \
1076                       Perl_ck_warner_d(aTHX_ packWARN2(WARN_DEPRECATED,     \
1077                                                       WARN_REGEXP),         \
1078                                              m REPORT_LOCATION,             \
1079                                              REPORT_LOCATION_ARGS(loc)))
1080
1081 #define ckWARN2reg_d(loc,m, a1)                                             \
1082     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                                \
1083                       Perl_ck_warner_d(aTHX_ packWARN(WARN_REGEXP),         \
1084                                             m REPORT_LOCATION,              \
1085                                             a1, REPORT_LOCATION_ARGS(loc)))
1086
1087 #define ckWARN2reg(loc, m, a1)                                              \
1088     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                                \
1089                       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),           \
1090                                           m REPORT_LOCATION,                \
1091                                           a1, REPORT_LOCATION_ARGS(loc)))
1092
1093 #define vWARN3(loc, m, a1, a2)                                              \
1094     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                                \
1095                       Perl_warner(aTHX_ packWARN(WARN_REGEXP),              \
1096                                        m REPORT_LOCATION,                   \
1097                                        a1, a2, REPORT_LOCATION_ARGS(loc)))
1098
1099 #define ckWARN3reg(loc, m, a1, a2)                                          \
1100     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                                \
1101                       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),           \
1102                                           m REPORT_LOCATION,                \
1103                                           a1, a2,                           \
1104                                           REPORT_LOCATION_ARGS(loc)))
1105
1106 #define vWARN4(loc, m, a1, a2, a3)                                      \
1107     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                            \
1108                       Perl_warner(aTHX_ packWARN(WARN_REGEXP),          \
1109                                        m REPORT_LOCATION,               \
1110                                        a1, a2, a3,                      \
1111                                        REPORT_LOCATION_ARGS(loc)))
1112
1113 #define ckWARN4reg(loc, m, a1, a2, a3)                                  \
1114     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                            \
1115                       Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),       \
1116                                           m REPORT_LOCATION,            \
1117                                           a1, a2, a3,                   \
1118                                           REPORT_LOCATION_ARGS(loc)))
1119
1120 #define vWARN5(loc, m, a1, a2, a3, a4)                                  \
1121     _WARN_HELPER(loc, packWARN(WARN_REGEXP),                            \
1122                       Perl_warner(aTHX_ packWARN(WARN_REGEXP),          \
1123                                        m REPORT_LOCATION,               \
1124                                        a1, a2, a3, a4,                  \
1125                                        REPORT_LOCATION_ARGS(loc)))
1126
1127 #define ckWARNexperimental(loc, class, m)                               \
1128     STMT_START {                                                        \
1129         if (! RExC_warned_ ## class) { /* warn once per compilation */  \
1130             RExC_warned_ ## class = 1;                                  \
1131             _WARN_HELPER(loc, packWARN(class),                          \
1132                       Perl_ck_warner_d(aTHX_ packWARN(class),           \
1133                                             m REPORT_LOCATION,          \
1134                                             REPORT_LOCATION_ARGS(loc)));\
1135         }                                                               \
1136     } STMT_END
1137
1138 #define ckWARNexperimental_with_arg(loc, class, m, arg)                 \
1139     STMT_START {                                                        \
1140         if (! RExC_warned_ ## class) { /* warn once per compilation */  \
1141             RExC_warned_ ## class = 1;                                  \
1142             _WARN_HELPER(loc, packWARN(class),                          \
1143                       Perl_ck_warner_d(aTHX_ packWARN(class),           \
1144                                        m REPORT_LOCATION,               \
1145                                        arg, REPORT_LOCATION_ARGS(loc)));\
1146         }                                                               \
1147     } STMT_END
1148
1149 /* Convert between a pointer to a node and its offset from the beginning of the
1150  * program */
1151 #define REGNODE_p(offset)    (RExC_emit_start + (offset))
1152 #define REGNODE_OFFSET(node) (__ASSERT_((node) >= RExC_emit_start)      \
1153                               (SSize_t) ((node) - RExC_emit_start))
1154
1155 #define ProgLen(ri) ri->proglen
1156 #define SetProgLen(ri,x) ri->proglen = x
1157
1158 #if PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS
1159 #define EXPERIMENTAL_INPLACESCAN
1160 #endif /*PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS*/
1161
1162 #define DEBUG_RExC_seen()                                                   \
1163         DEBUG_OPTIMISE_MORE_r({                                             \
1164             Perl_re_printf( aTHX_ "RExC_seen: ");                           \
1165                                                                             \
1166             if (RExC_seen & REG_ZERO_LEN_SEEN)                              \
1167                 Perl_re_printf( aTHX_ "REG_ZERO_LEN_SEEN ");                \
1168                                                                             \
1169             if (RExC_seen & REG_LOOKBEHIND_SEEN)                            \
1170                 Perl_re_printf( aTHX_ "REG_LOOKBEHIND_SEEN ");              \
1171                                                                             \
1172             if (RExC_seen & REG_GPOS_SEEN)                                  \
1173                 Perl_re_printf( aTHX_ "REG_GPOS_SEEN ");                    \
1174                                                                             \
1175             if (RExC_seen & REG_RECURSE_SEEN)                               \
1176                 Perl_re_printf( aTHX_ "REG_RECURSE_SEEN ");                 \
1177                                                                             \
1178             if (RExC_seen & REG_TOP_LEVEL_BRANCHES_SEEN)                    \
1179                 Perl_re_printf( aTHX_ "REG_TOP_LEVEL_BRANCHES_SEEN ");      \
1180                                                                             \
1181             if (RExC_seen & REG_VERBARG_SEEN)                               \
1182                 Perl_re_printf( aTHX_ "REG_VERBARG_SEEN ");                 \
1183                                                                             \
1184             if (RExC_seen & REG_CUTGROUP_SEEN)                              \
1185                 Perl_re_printf( aTHX_ "REG_CUTGROUP_SEEN ");                \
1186                                                                             \
1187             if (RExC_seen & REG_RUN_ON_COMMENT_SEEN)                        \
1188                 Perl_re_printf( aTHX_ "REG_RUN_ON_COMMENT_SEEN ");          \
1189                                                                             \
1190             if (RExC_seen & REG_UNFOLDED_MULTI_SEEN)                        \
1191                 Perl_re_printf( aTHX_ "REG_UNFOLDED_MULTI_SEEN ");          \
1192                                                                             \
1193             if (RExC_seen & REG_UNBOUNDED_QUANTIFIER_SEEN)                  \
1194                 Perl_re_printf( aTHX_ "REG_UNBOUNDED_QUANTIFIER_SEEN ");    \
1195                                                                             \
1196             if (RExC_seen & REG_PESSIMIZE_SEEN)                             \
1197                 Perl_re_printf( aTHX_ "REG_PESSIMIZE_SEEN ");               \
1198                                                                             \
1199             Perl_re_printf( aTHX_ "\n");                                    \
1200         });
1201
1202 #define DEBUG_SHOW_STUDY_FLAG(flags,flag) \
1203   if ((flags) & flag) Perl_re_printf( aTHX_  "%s ", #flag)
1204
1205
1206 #ifdef DEBUGGING
1207 #  define DEBUG_STUDYDATA(where, data, depth, is_inf, min, stopmin, delta) \
1208                     debug_studydata(where, data, depth, is_inf, min, stopmin, delta)
1209
1210 #  define DEBUG_PEEP(str, scan, depth, flags)   \
1211                     debug_peep(str, pRExC_state, scan, depth, flags)
1212 #else
1213 #  define DEBUG_STUDYDATA(where, data, depth, is_inf, min, stopmin, delta) NOOP
1214 #  define DEBUG_PEEP(str, scan, depth, flags)         NOOP
1215 #endif
1216
1217 #define REGTAIL(x,y,z) regtail((x),(y),(z),depth+1)
1218 #ifdef DEBUGGING
1219 #define REGTAIL_STUDY(x,y,z) regtail_study((x),(y),(z),depth+1)
1220 #else
1221 #define REGTAIL_STUDY(x,y,z) regtail((x),(y),(z),depth+1)
1222 #endif
1223
1224 #define MADE_TRIE       1
1225 #define MADE_JUMP_TRIE  2
1226 #define MADE_EXACT_TRIE 4
1227
1228 #define INVLIST_INDEX                   0
1229 #define ONLY_LOCALE_MATCHES_INDEX       1
1230 #define DEFERRED_USER_DEFINED_INDEX     2
1231
1232 /* These two functions currently do the exact same thing */
1233 #define ssc_init_zero           ssc_init
1234
1235 #define ssc_add_cp(ssc, cp)   ssc_add_range((ssc), (cp), (cp))
1236 #define ssc_match_all_cp(ssc) ssc_add_range(ssc, 0, UV_MAX)
1237
1238 #ifdef DEBUGGING
1239 #define REGNODE_GUTS(state,op,extra_size) \
1240     regnode_guts_debug(state,op,extra_size)
1241 #else
1242 #define REGNODE_GUTS(state,op,extra_size) \
1243     regnode_guts(state,extra_size)
1244 #endif
1245
1246 #define CLEAR_OPTSTART                                                          \
1247     if (optstart) STMT_START {                                                  \
1248         DEBUG_OPTIMISE_r(Perl_re_printf( aTHX_                                  \
1249                               " (%" IVdf " nodes)\n", (IV)(node - optstart)));  \
1250         optstart=NULL;                                                          \
1251     } STMT_END
1252
1253 #define DUMPUNTIL(b,e)                                          \
1254     CLEAR_OPTSTART;                                             \
1255     node = dumpuntil(r,start,(b),(e),last,sv,indent+1,depth+1);
1256
1257 #define REGNODE_STEP_OVER(ret,t1,t2) \
1258     NEXT_OFF(REGNODE_p(ret)) = ((sizeof(t1)+sizeof(t2))/sizeof(regnode))
1259
1260 #endif /* REGCOMP_INTERNAL_H */