This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Faster feature checks
[perl5.git] / feature.h
1 /* -*- buffer-read-only: t -*-
2    !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
3    This file is built by regen/feature.pl.
4    Any changes made here will be lost!
5  */
6
7
8 #ifndef PERL_FEATURE_H_
9 #define PERL_FEATURE_H_
10
11 #if defined(PERL_CORE) || defined (PERL_EXT)
12
13 #define HINT_FEATURE_SHIFT      26
14
15 #define FEATURE_BITWISE_BIT         0x0001
16 #define FEATURE___SUB___BIT         0x0002
17 #define FEATURE_MYREF_BIT           0x0004
18 #define FEATURE_EVALBYTES_BIT       0x0008
19 #define FEATURE_FC_BIT              0x0010
20 #define FEATURE_POSTDEREF_QQ_BIT    0x0020
21 #define FEATURE_REFALIASING_BIT     0x0040
22 #define FEATURE_SAY_BIT             0x0080
23 #define FEATURE_SIGNATURES_BIT      0x0100
24 #define FEATURE_STATE_BIT           0x0200
25 #define FEATURE_SWITCH_BIT          0x0400
26 #define FEATURE_UNIEVAL_BIT         0x0800
27 #define FEATURE_UNICODE_BIT         0x1000
28
29 #define FEATURE_BUNDLE_DEFAULT  0
30 #define FEATURE_BUNDLE_510      1
31 #define FEATURE_BUNDLE_511      2
32 #define FEATURE_BUNDLE_515      3
33 #define FEATURE_BUNDLE_523      4
34 #define FEATURE_BUNDLE_527      5
35 #define FEATURE_BUNDLE_CUSTOM   (HINT_FEATURE_MASK >> HINT_FEATURE_SHIFT)
36
37 #define CURRENT_HINTS \
38     (PL_curcop == &PL_compiling ? PL_hints : PL_curcop->cop_hints)
39 #define CURRENT_FEATURE_BUNDLE \
40     ((CURRENT_HINTS & HINT_FEATURE_MASK) >> HINT_FEATURE_SHIFT)
41
42 /* Avoid using ... && Perl_feature_is_enabled(...) as that triggers a bug in
43    the HP-UX cc on PA-RISC */
44 #define FEATURE_IS_ENABLED(name)                                        \
45         ((CURRENT_HINTS                                                  \
46            & HINT_LOCALIZE_HH)                                            \
47             ? Perl_feature_is_enabled(aTHX_ STR_WITH_LEN(name)) : FALSE)
48
49 #define FEATURE_IS_ENABLED_MASK(mask)                   \
50   ((CURRENT_HINTS & HINT_LOCALIZE_HH)                \
51     ? (PL_curcop->cop_features & (mask)) : FALSE)
52
53 /* The longest string we pass in.  */
54 #define MAX_FEATURE_LEN (sizeof("postderef_qq")-1)
55
56 #define FEATURE_FC_IS_ENABLED \
57     ( \
58         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_515 && \
59          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
60      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
61          FEATURE_IS_ENABLED_MASK(FEATURE_FC_BIT)) \
62     )
63
64 #define FEATURE_SAY_IS_ENABLED \
65     ( \
66         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_510 && \
67          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
68      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
69          FEATURE_IS_ENABLED_MASK(FEATURE_SAY_BIT)) \
70     )
71
72 #define FEATURE_STATE_IS_ENABLED \
73     ( \
74         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_510 && \
75          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
76      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
77          FEATURE_IS_ENABLED_MASK(FEATURE_STATE_BIT)) \
78     )
79
80 #define FEATURE_SWITCH_IS_ENABLED \
81     ( \
82         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_510 && \
83          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
84      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
85          FEATURE_IS_ENABLED_MASK(FEATURE_SWITCH_BIT)) \
86     )
87
88 #define FEATURE_BITWISE_IS_ENABLED \
89     ( \
90         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_527 \
91      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
92          FEATURE_IS_ENABLED_MASK(FEATURE_BITWISE_BIT)) \
93     )
94
95 #define FEATURE_EVALBYTES_IS_ENABLED \
96     ( \
97         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_515 && \
98          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
99      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
100          FEATURE_IS_ENABLED_MASK(FEATURE_EVALBYTES_BIT)) \
101     )
102
103 #define FEATURE_SIGNATURES_IS_ENABLED \
104     ( \
105         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
106          FEATURE_IS_ENABLED_MASK(FEATURE_SIGNATURES_BIT) \
107     )
108
109 #define FEATURE___SUB___IS_ENABLED \
110     ( \
111         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_515 && \
112          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
113      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
114          FEATURE_IS_ENABLED_MASK(FEATURE___SUB___BIT)) \
115     )
116
117 #define FEATURE_REFALIASING_IS_ENABLED \
118     ( \
119         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
120          FEATURE_IS_ENABLED_MASK(FEATURE_REFALIASING_BIT) \
121     )
122
123 #define FEATURE_POSTDEREF_QQ_IS_ENABLED \
124     ( \
125         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_523 && \
126          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
127      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
128          FEATURE_IS_ENABLED_MASK(FEATURE_POSTDEREF_QQ_BIT)) \
129     )
130
131 #define FEATURE_UNIEVAL_IS_ENABLED \
132     ( \
133         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_515 && \
134          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
135      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
136          FEATURE_IS_ENABLED_MASK(FEATURE_UNIEVAL_BIT)) \
137     )
138
139 #define FEATURE_MYREF_IS_ENABLED \
140     ( \
141         CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
142          FEATURE_IS_ENABLED_MASK(FEATURE_MYREF_BIT) \
143     )
144
145 #define FEATURE_UNICODE_IS_ENABLED \
146     ( \
147         (CURRENT_FEATURE_BUNDLE >= FEATURE_BUNDLE_511 && \
148          CURRENT_FEATURE_BUNDLE <= FEATURE_BUNDLE_527) \
149      || (CURRENT_FEATURE_BUNDLE == FEATURE_BUNDLE_CUSTOM && \
150          FEATURE_IS_ENABLED_MASK(FEATURE_UNICODE_BIT)) \
151     )
152
153
154 #define SAVEFEATUREBITS() SAVEI32(PL_compiling.cop_features)
155
156 #define CLEARFEATUREBITS() (PL_compiling.cop_features = 0)
157
158 #define STOREFEATUREBITSHH(hh) \
159   (hv_stores((hh), "feature/bits", newSVuv(PL_compiling.cop_features)))
160
161 #define FETCHFEATUREBITSHH(hh)                              \
162   STMT_START {                                              \
163       SV **fbsv = hv_fetchs((hh), "feature/bits", FALSE);   \
164       PL_compiling.cop_features = fbsv ? SvUV(*fbsv) : 0;   \
165   } STMT_END
166
167 #endif /* PERL_CORE or PERL_EXT */
168
169 #ifdef PERL_IN_OP_C
170 PERL_STATIC_INLINE void
171 S_enable_feature_bundle(pTHX_ SV *ver)
172 {
173     SV *comp_ver = sv_newmortal();
174     PL_hints = (PL_hints &~ HINT_FEATURE_MASK)
175              | (
176                   (sv_setnv(comp_ver, 5.027),
177                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
178                         ? FEATURE_BUNDLE_527 :
179                   (sv_setnv(comp_ver, 5.023),
180                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
181                         ? FEATURE_BUNDLE_523 :
182                   (sv_setnv(comp_ver, 5.015),
183                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
184                         ? FEATURE_BUNDLE_515 :
185                   (sv_setnv(comp_ver, 5.011),
186                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
187                         ? FEATURE_BUNDLE_511 :
188                   (sv_setnv(comp_ver, 5.009005),
189                    vcmp(ver, upg_version(comp_ver, FALSE)) >= 0)
190                         ? FEATURE_BUNDLE_510 :
191                           FEATURE_BUNDLE_DEFAULT
192                ) << HINT_FEATURE_SHIFT;
193     /* special case */
194     assert(PL_curcop == &PL_compiling);
195     if (FEATURE_UNICODE_IS_ENABLED) PL_hints |=  HINT_UNI_8_BIT;
196     else                            PL_hints &= ~HINT_UNI_8_BIT;
197 }
198 #endif /* PERL_IN_OP_C */
199
200 #endif /* PERL_FEATURE_H_ */
201
202 /* ex: set ro: */