This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta: no entry is necessary for c1b879e57aecb
[perl5.git] / op_reg_common.h
CommitLineData
1850c8f9
KW
1/* op_reg_common.h
2 *
3 * Definitions common to by op.h and regexp.h
4 *
5 * Copyright (C) 2010 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
5b126c84
KW
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 */
da494bb7 21/* Make sure to update ext/re/re.pm when changing this! */
93136d17
KW
22#ifndef RXf_PMf_STD_PMMOD_SHIFT /* Only expand #include of this file once */
23
5b126c84
KW
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 */
1e215989 29/* Make sure to update lib/re.pm when changing these! */
5b126c84
KW
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 */
a62b1201
KW
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 */
da494bb7 39/* Make sure to update ext/re/re.pm when changing this! */
a62b1201
KW
40typedef enum {
41 REGEX_DEPENDS_CHARSET = 0,
42 REGEX_LOCALE_CHARSET,
cfaf538b
KW
43 REGEX_UNICODE_CHARSET,
44 REGEX_ASCII_RESTRICTED_CHARSET
a62b1201
KW
45} regex_charset;
46
47#define _RXf_PMf_CHARSET_SHIFT ((RXf_PMf_STD_PMMOD_SHIFT)+5)
48#define RXf_PMf_CHARSET (3 << (_RXf_PMf_CHARSET_SHIFT)) /* 2 bits */
49
50/* embed.pl doesn't yet know how to handle static inline functions, so
51 manually decorate them here with gcc-style attributes.
52*/
53PERL_STATIC_INLINE void
54set_regex_charset(U32 * const flags, const regex_charset cs)
55 __attribute__nonnull__(1);
56
57PERL_STATIC_INLINE void
58set_regex_charset(U32 * const flags, const regex_charset cs)
59{
60 /* Sets the character set portion of 'flags' to 'cs', which is a member of
61 * the above enum */
62
63 *flags &= ~RXf_PMf_CHARSET;
64 *flags |= (cs << _RXf_PMf_CHARSET_SHIFT);
65}
66
67PERL_STATIC_INLINE regex_charset
68get_regex_charset(const U32 flags)
69 __attribute__warn_unused_result__;
70
71PERL_STATIC_INLINE regex_charset
72get_regex_charset(const U32 flags)
73{
74 /* Returns the enum corresponding to the character set in 'flags' */
75
bfba585a 76 return (regex_charset) ((flags & RXf_PMf_CHARSET) >> _RXf_PMf_CHARSET_SHIFT);
a62b1201 77}
1850c8f9 78
3214c85f
KW
79/* Next available bit after the above. Name begins with '_' so won't be
80 * exported by B */
9de15fec 81#define _RXf_PMf_SHIFT_NEXT (RXf_PMf_STD_PMMOD_SHIFT+7)
bb1c6009 82
5b126c84
KW
83/* Mask of the above bits. These need to be transferred from op_pmflags to
84 * re->extflags during compilation */
a62b1201 85#define RXf_PMf_COMPILETIME (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_CHARSET|RXf_PMf_FOLD|RXf_PMf_EXTENDED|RXf_PMf_KEEPCOPY)
bb1c6009
KW
86
87/* These copies need to be numerical or defsubs_h.PL won't know about them. */
88#define PMf_MULTILINE 1<<0
89#define PMf_SINGLELINE 1<<1
90#define PMf_FOLD 1<<2
91#define PMf_EXTENDED 1<<3
92#define PMf_KEEPCOPY 1<<4
bb1c6009 93
a62b1201 94#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
bb1c6009
KW
95# error RXf_PMf defines are wrong
96#endif
5b126c84
KW
97
98#define PMf_COMPILETIME RXf_PMf_COMPILETIME
99
100/* Error check that haven't left something out of this. This isn't done
101 * directly in the #define because doing so confuses regcomp.pl.
102 * (2**n - 1) is n 1 bits, so the below gets the contiguous bits between the
103 * beginning and ending shifts */
104#if RXf_PMf_COMPILETIME != (((1 << (_RXf_PMf_SHIFT_NEXT))-1) \
105 & (~((1 << RXf_PMf_STD_PMMOD_SHIFT)-1)))
106# error RXf_PMf_COMPILETIME is invalid
107#endif
93136d17
KW
108
109#endif /* Include only once */