This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge topic branch into blead
[perl5.git] / op_reg_common.h
1 /*    op_reg_common.h
2  *
3  *    Definitions common to by op.h and regexp.h
4  *
5  *    Copyright (C) 2010, 2011 by Larry Wall and others
6  *
7  *    You may distribute under the terms of either the GNU General Public
8  *    License or the Artistic License, as specified in the README file.
9  *
10  */
11
12 /* These defines are used in both op.h and regexp.h  The definitions use the
13  * shift form so that ext/B/defsubs_h.PL will pick them up.
14  *
15  * Data structures used in the two headers have common fields, and in fact one
16  * is copied onto the other.  This makes it easy to keep them in sync */
17
18 /* This tells where the first of these bits is.  Setting it to 0 saved cycles
19  * and memory.  I (khw) think the code will work if changed back, but haven't
20  * tested it */
21 /* Make sure to update ext/re/re.pm when changing this! */
22 #ifndef RXf_PMf_STD_PMMOD_SHIFT /* Only expand #include of this file once */
23
24 #define RXf_PMf_STD_PMMOD_SHIFT 0
25
26 /* The bits need to be ordered so that the msix are contiguous starting at bit
27  * RXf_PMf_STD_PMMOD_SHIFT, followed by the p.  See STD_PAT_MODS and
28  * INT_PAT_MODS in regexp.h for the reason contiguity is needed */
29 /* Make sure to update lib/re.pm when changing these! */
30 #define RXf_PMf_MULTILINE      (1 << (RXf_PMf_STD_PMMOD_SHIFT+0))    /* /m */
31 #define RXf_PMf_SINGLELINE     (1 << (RXf_PMf_STD_PMMOD_SHIFT+1))    /* /s */
32 #define RXf_PMf_FOLD           (1 << (RXf_PMf_STD_PMMOD_SHIFT+2))    /* /i */
33 #define RXf_PMf_EXTENDED       (1 << (RXf_PMf_STD_PMMOD_SHIFT+3))    /* /x */
34 #define RXf_PMf_KEEPCOPY       (1 << (RXf_PMf_STD_PMMOD_SHIFT+4))    /* /p */
35
36 /* The character set for the regex is stored in a field of more than one bit
37  * using an enum, for reasons of compactness and to ensure that the options are
38  * mutually exclusive */
39 /* Make sure to update ext/re/re.pm and regcomp.sym (as these are used as
40  * offsets for various node types, like SPACE vs SPACEL, etc) when changing
41  * this! */
42 typedef enum {
43     REGEX_DEPENDS_CHARSET = 0,
44     REGEX_LOCALE_CHARSET,
45     REGEX_UNICODE_CHARSET,
46     REGEX_ASCII_RESTRICTED_CHARSET,
47     REGEX_ASCII_MORE_RESTRICTED_CHARSET
48 } regex_charset;
49
50 #define _RXf_PMf_CHARSET_SHIFT ((RXf_PMf_STD_PMMOD_SHIFT)+5)
51 #define RXf_PMf_CHARSET (7 << (_RXf_PMf_CHARSET_SHIFT)) /* 3 bits */
52
53 /* Manually decorate these functions here with gcc-style attributes just to
54  * avoid making the regex_charset typedef global, which it would need to be for
55  * proto.h to understand it */
56 PERL_STATIC_INLINE void
57 set_regex_charset(U32 * const flags, const regex_charset cs)
58     __attribute__nonnull__(1);
59
60 PERL_STATIC_INLINE void
61 set_regex_charset(U32 * const flags, const regex_charset cs)
62 {
63     /* Sets the character set portion of 'flags' to 'cs', which is a member of
64      * the above enum */
65
66     *flags &= ~RXf_PMf_CHARSET;
67     *flags |= (cs << _RXf_PMf_CHARSET_SHIFT);
68 }
69
70 PERL_STATIC_INLINE regex_charset
71 get_regex_charset(const U32 flags)
72     __attribute__warn_unused_result__;
73
74 PERL_STATIC_INLINE regex_charset
75 get_regex_charset(const U32 flags)
76 {
77     /* Returns the enum corresponding to the character set in 'flags' */
78
79     return (regex_charset) ((flags & RXf_PMf_CHARSET) >> _RXf_PMf_CHARSET_SHIFT);
80 }
81
82 /* Next available bit after the above.  Name begins with '_' so won't be
83  * exported by B */
84 #define _RXf_PMf_SHIFT_NEXT (RXf_PMf_STD_PMMOD_SHIFT+8)
85
86 /* Mask of the above bits.  These need to be transferred from op_pmflags to
87  * re->extflags during compilation */
88 #define RXf_PMf_COMPILETIME    (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_CHARSET|RXf_PMf_FOLD|RXf_PMf_EXTENDED|RXf_PMf_KEEPCOPY)
89
90 #if RXf_PMf_COMPILETIME > 255
91 #  error RXf_PMf_COMPILETIME wont fit in U8 flags field of eval node
92 #endif
93
94 /* These copies need to be numerical or defsubs_h.PL won't know about them. */
95 #define PMf_MULTILINE    1<<0
96 #define PMf_SINGLELINE   1<<1
97 #define PMf_FOLD         1<<2
98 #define PMf_EXTENDED     1<<3
99 #define PMf_KEEPCOPY     1<<4
100
101 #if PMf_MULTILINE != RXf_PMf_MULTILINE || PMf_SINGLELINE != RXf_PMf_SINGLELINE || PMf_FOLD != RXf_PMf_FOLD || PMf_EXTENDED != RXf_PMf_EXTENDED || PMf_KEEPCOPY != RXf_PMf_KEEPCOPY
102 #   error RXf_PMf defines are wrong
103 #endif
104
105 #define PMf_COMPILETIME RXf_PMf_COMPILETIME
106
107 /*  Error check that haven't left something out of this.  This isn't done
108  *  directly in the #define because doing so confuses regcomp.pl.
109  *  (2**n - 1) is n 1 bits, so the below gets the contiguous bits between the
110  *  beginning and ending shifts */
111 #if RXf_PMf_COMPILETIME != (((1 << (_RXf_PMf_SHIFT_NEXT))-1) \
112                             & (~((1 << RXf_PMf_STD_PMMOD_SHIFT)-1)))
113 #   error RXf_PMf_COMPILETIME is invalid
114 #endif
115
116 #endif /* Include only once */