/src/php-src/Zend/zend_enum.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Ilija Tovilo <ilutov@php.net> | |
16 | | +----------------------------------------------------------------------+ |
17 | | */ |
18 | | |
19 | | #include "zend.h" |
20 | | #include "zend_API.h" |
21 | | #include "zend_compile.h" |
22 | | #include "zend_enum_arginfo.h" |
23 | | #include "zend_interfaces.h" |
24 | | #include "zend_enum.h" |
25 | | #include "zend_extensions.h" |
26 | | #include "zend_observer.h" |
27 | | |
28 | | #define ZEND_ENUM_DISALLOW_MAGIC_METHOD(propertyName, methodName) \ |
29 | 15.0k | do { \ |
30 | 15.0k | if (ce->propertyName) { \ |
31 | 75 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), methodName); \ |
32 | 75 | } \ |
33 | 15.0k | } while (0); |
34 | | |
35 | | ZEND_API zend_class_entry *zend_ce_unit_enum; |
36 | | ZEND_API zend_class_entry *zend_ce_backed_enum; |
37 | | ZEND_API zend_object_handlers zend_enum_object_handlers; |
38 | | |
39 | | zend_object *zend_enum_new(zval *result, zend_class_entry *ce, zend_string *case_name, zval *backing_value_zv) |
40 | 1.59k | { |
41 | 1.59k | zend_object *zobj = zend_objects_new(ce); |
42 | 1.59k | GC_ADD_FLAGS(zobj, GC_NOT_COLLECTABLE); |
43 | 1.59k | ZVAL_OBJ(result, zobj); |
44 | | |
45 | 1.59k | zval *zname = OBJ_PROP_NUM(zobj, 0); |
46 | 1.59k | ZVAL_STR_COPY(zname, case_name); |
47 | | /* ZVAL_COPY does not set Z_PROP_FLAG, this needs to be cleared to avoid leaving IS_PROP_REINITABLE set */ |
48 | 1.59k | Z_PROP_FLAG_P(zname) = 0; |
49 | | |
50 | 1.59k | if (backing_value_zv != NULL) { |
51 | 736 | zval *prop = OBJ_PROP_NUM(zobj, 1); |
52 | | |
53 | 736 | ZVAL_COPY(prop, backing_value_zv); |
54 | | /* ZVAL_COPY does not set Z_PROP_FLAG, this needs to be cleared to avoid leaving IS_PROP_REINITABLE set */ |
55 | 736 | Z_PROP_FLAG_P(prop) = 0; |
56 | 736 | } |
57 | | |
58 | 1.59k | return zobj; |
59 | 1.59k | } |
60 | | |
61 | | static void zend_verify_enum_properties(const zend_class_entry *ce) |
62 | 1.41k | { |
63 | 1.41k | const zend_property_info *property_info; |
64 | | |
65 | 6.63k | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, property_info) { |
66 | 6.63k | if (zend_string_equals(property_info->name, ZSTR_KNOWN(ZEND_STR_NAME))) { |
67 | 1.41k | continue; |
68 | 1.41k | } |
69 | 484 | if ( |
70 | 484 | ce->enum_backing_type != IS_UNDEF |
71 | 476 | && zend_string_equals(property_info->name, ZSTR_KNOWN(ZEND_STR_VALUE)) |
72 | 484 | ) { |
73 | 476 | continue; |
74 | 476 | } |
75 | | // FIXME: File/line number for traits? |
76 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", |
77 | 8 | ZSTR_VAL(ce->name)); |
78 | 484 | } ZEND_HASH_FOREACH_END(); |
79 | 1.41k | } |
80 | | |
81 | | static void zend_verify_enum_magic_methods(const zend_class_entry *ce) |
82 | 1.40k | { |
83 | | // Only __get, __call and __invoke are allowed |
84 | | |
85 | 1.40k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(constructor, "__construct"); |
86 | 1.39k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(destructor, "__destruct"); |
87 | 1.38k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(clone, "__clone"); |
88 | 1.38k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__get, "__get"); |
89 | 1.37k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__set, "__set"); |
90 | 1.36k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unset, "__unset"); |
91 | 1.36k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__isset, "__isset"); |
92 | 1.35k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__tostring, "__toString"); |
93 | 1.35k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__debugInfo, "__debugInfo"); |
94 | 1.34k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__serialize, "__serialize"); |
95 | 1.33k | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unserialize, "__unserialize"); |
96 | | |
97 | 1.33k | static const char *const forbidden_methods[] = { |
98 | 1.33k | "__sleep", |
99 | 1.33k | "__wakeup", |
100 | 1.33k | "__set_state", |
101 | 1.33k | }; |
102 | | |
103 | 1.33k | uint32_t forbidden_methods_length = sizeof(forbidden_methods) / sizeof(forbidden_methods[0]); |
104 | 5.29k | for (uint32_t i = 0; i < forbidden_methods_length; ++i) { |
105 | 3.98k | const char *forbidden_method = forbidden_methods[i]; |
106 | | |
107 | 3.98k | if (zend_hash_str_exists(&ce->function_table, forbidden_method, strlen(forbidden_method))) { |
108 | 15 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), forbidden_method); |
109 | 15 | } |
110 | 3.98k | } |
111 | 1.33k | } |
112 | | |
113 | | static void zend_verify_enum_interfaces(const zend_class_entry *ce) |
114 | 1.31k | { |
115 | 1.31k | if (zend_class_implements_interface(ce, zend_ce_serializable)) { |
116 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
117 | 5 | "Enum %s cannot implement the Serializable interface", ZSTR_VAL(ce->name)); |
118 | 5 | } |
119 | 1.31k | } |
120 | | |
121 | | void zend_verify_enum(const zend_class_entry *ce) |
122 | 1.41k | { |
123 | 1.41k | zend_verify_enum_properties(ce); |
124 | 1.41k | zend_verify_enum_magic_methods(ce); |
125 | 1.41k | zend_verify_enum_interfaces(ce); |
126 | 1.41k | } |
127 | | |
128 | | static int zend_implement_unit_enum(zend_class_entry *interface, zend_class_entry *class_type) |
129 | 1.51k | { |
130 | 1.51k | if (class_type->ce_flags & ZEND_ACC_ENUM) { |
131 | 1.50k | return SUCCESS; |
132 | 1.50k | } |
133 | | |
134 | 6 | zend_error_noreturn(E_ERROR, "Non-enum class %s cannot implement interface %s", |
135 | 6 | ZSTR_VAL(class_type->name), |
136 | 6 | ZSTR_VAL(interface->name)); |
137 | | |
138 | 0 | return FAILURE; |
139 | 1.51k | } |
140 | | |
141 | | static int zend_implement_backed_enum(zend_class_entry *interface, zend_class_entry *class_type) |
142 | 507 | { |
143 | 507 | if (!(class_type->ce_flags & ZEND_ACC_ENUM)) { |
144 | 6 | zend_error_noreturn(E_ERROR, "Non-enum class %s cannot implement interface %s", |
145 | 6 | ZSTR_VAL(class_type->name), |
146 | 6 | ZSTR_VAL(interface->name)); |
147 | 0 | return FAILURE; |
148 | 6 | } |
149 | | |
150 | 501 | if (class_type->enum_backing_type == IS_UNDEF) { |
151 | 6 | zend_error_noreturn(E_ERROR, "Non-backed enum %s cannot implement interface %s", |
152 | 6 | ZSTR_VAL(class_type->name), |
153 | 6 | ZSTR_VAL(interface->name)); |
154 | 0 | return FAILURE; |
155 | 6 | } |
156 | | |
157 | 495 | return SUCCESS; |
158 | 501 | } |
159 | | |
160 | | void zend_register_enum_ce(void) |
161 | 16 | { |
162 | 16 | zend_ce_unit_enum = register_class_UnitEnum(); |
163 | 16 | zend_ce_unit_enum->interface_gets_implemented = zend_implement_unit_enum; |
164 | | |
165 | 16 | zend_ce_backed_enum = register_class_BackedEnum(zend_ce_unit_enum); |
166 | 16 | zend_ce_backed_enum->interface_gets_implemented = zend_implement_backed_enum; |
167 | | |
168 | 16 | memcpy(&zend_enum_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); |
169 | 16 | zend_enum_object_handlers.clone_obj = NULL; |
170 | 16 | zend_enum_object_handlers.compare = zend_objects_not_comparable; |
171 | 16 | } |
172 | | |
173 | | void zend_enum_add_interfaces(zend_class_entry *ce) |
174 | 5.84k | { |
175 | 5.84k | uint32_t num_interfaces_before = ce->num_interfaces; |
176 | | |
177 | 5.84k | ce->num_interfaces++; |
178 | 5.84k | if (ce->enum_backing_type != IS_UNDEF) { |
179 | 732 | ce->num_interfaces++; |
180 | 732 | } |
181 | | |
182 | 5.84k | ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)); |
183 | | |
184 | 5.84k | ce->interface_names = erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
185 | | |
186 | 5.84k | ce->interface_names[num_interfaces_before].name = zend_string_copy(zend_ce_unit_enum->name); |
187 | 5.84k | ce->interface_names[num_interfaces_before].lc_name = ZSTR_INIT_LITERAL("unitenum", 0); |
188 | | |
189 | 5.84k | if (ce->enum_backing_type != IS_UNDEF) { |
190 | 732 | ce->interface_names[num_interfaces_before + 1].name = zend_string_copy(zend_ce_backed_enum->name); |
191 | 732 | ce->interface_names[num_interfaces_before + 1].lc_name = ZSTR_INIT_LITERAL("backedenum", 0); |
192 | 732 | } |
193 | | |
194 | 5.84k | ce->default_object_handlers = &zend_enum_object_handlers; |
195 | 5.84k | } |
196 | | |
197 | | zend_result zend_enum_build_backed_enum_table(zend_class_entry *ce) |
198 | 357 | { |
199 | 357 | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM); |
200 | 357 | ZEND_ASSERT(ce->type == ZEND_USER_CLASS); |
201 | | |
202 | 357 | uint32_t backing_type = ce->enum_backing_type; |
203 | 357 | ZEND_ASSERT(backing_type != IS_UNDEF); |
204 | | |
205 | 357 | HashTable *backed_enum_table = emalloc(sizeof(HashTable)); |
206 | 357 | zend_hash_init(backed_enum_table, 0, NULL, ZVAL_PTR_DTOR, 0); |
207 | 357 | zend_class_set_backed_enum_table(ce, backed_enum_table); |
208 | | |
209 | 357 | const zend_string *enum_class_name = ce->name; |
210 | | |
211 | 357 | zend_string *name; |
212 | 357 | zval *val; |
213 | 2.16k | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(CE_CONSTANTS_TABLE(ce), name, val) { |
214 | 2.16k | zend_class_constant *c = Z_PTR_P(val); |
215 | 2.16k | if ((ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE) == 0) { |
216 | 10 | continue; |
217 | 10 | } |
218 | | |
219 | 716 | zval *c_value = &c->value; |
220 | 716 | zval *case_name = zend_enum_fetch_case_name(Z_OBJ_P(c_value)); |
221 | 716 | zval *case_value = zend_enum_fetch_case_value(Z_OBJ_P(c_value)); |
222 | | |
223 | 716 | if (ce->enum_backing_type != Z_TYPE_P(case_value)) { |
224 | 38 | zend_type_error("Enum case type %s does not match enum backing type %s", |
225 | 38 | zend_get_type_by_const(Z_TYPE_P(case_value)), |
226 | 38 | zend_get_type_by_const(ce->enum_backing_type)); |
227 | 38 | goto failure; |
228 | 38 | } |
229 | | |
230 | 678 | if (ce->enum_backing_type == IS_LONG) { |
231 | 236 | zend_long long_key = Z_LVAL_P(case_value); |
232 | 236 | const zval *existing_case_name = zend_hash_index_find(backed_enum_table, long_key); |
233 | 236 | if (existing_case_name) { |
234 | 26 | zend_throw_error(NULL, "Duplicate value in enum %s for cases %s and %s", |
235 | 26 | ZSTR_VAL(enum_class_name), |
236 | 26 | Z_STRVAL_P(existing_case_name), |
237 | 26 | ZSTR_VAL(name)); |
238 | 26 | goto failure; |
239 | 26 | } |
240 | 210 | Z_TRY_ADDREF_P(case_name); |
241 | 210 | zend_hash_index_add_new(backed_enum_table, long_key, case_name); |
242 | 442 | } else { |
243 | 442 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
244 | 442 | zend_string *string_key = Z_STR_P(case_value); |
245 | 442 | const zval *existing_case_name = zend_hash_find(backed_enum_table, string_key); |
246 | 442 | if (existing_case_name != NULL) { |
247 | 25 | zend_throw_error(NULL, "Duplicate value in enum %s for cases %s and %s", |
248 | 25 | ZSTR_VAL(enum_class_name), |
249 | 25 | Z_STRVAL_P(existing_case_name), |
250 | 25 | ZSTR_VAL(name)); |
251 | 25 | goto failure; |
252 | 25 | } |
253 | 417 | Z_TRY_ADDREF_P(case_name); |
254 | 417 | zend_hash_add_new(backed_enum_table, string_key, case_name); |
255 | 417 | } |
256 | 678 | } ZEND_HASH_FOREACH_END(); |
257 | | |
258 | 268 | return SUCCESS; |
259 | | |
260 | 89 | failure: |
261 | 89 | zend_hash_release(backed_enum_table); |
262 | 89 | zend_class_set_backed_enum_table(ce, NULL); |
263 | 89 | return FAILURE; |
264 | 357 | } |
265 | | |
266 | | static ZEND_NAMED_FUNCTION(zend_enum_cases_func) |
267 | 27 | { |
268 | 27 | zend_class_entry *ce = execute_data->func->common.scope; |
269 | 27 | zend_class_constant *c; |
270 | | |
271 | 27 | ZEND_PARSE_PARAMETERS_NONE(); |
272 | | |
273 | 27 | array_init(return_value); |
274 | | |
275 | 232 | ZEND_HASH_MAP_FOREACH_PTR(CE_CONSTANTS_TABLE(ce), c) { |
276 | 232 | if (!(ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE)) { |
277 | 8 | continue; |
278 | 8 | } |
279 | 81 | zval *zv = &c->value; |
280 | 81 | if (Z_TYPE_P(zv) == IS_CONSTANT_AST) { |
281 | 81 | if (zval_update_constant_ex(zv, c->ce) == FAILURE) { |
282 | 0 | RETURN_THROWS(); |
283 | 0 | } |
284 | 81 | } |
285 | 81 | Z_ADDREF_P(zv); |
286 | 81 | zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), zv); |
287 | 81 | } ZEND_HASH_FOREACH_END(); |
288 | 27 | } |
289 | | |
290 | | ZEND_API zend_result zend_enum_get_case_by_value(zend_object **result, zend_class_entry *ce, zend_long long_key, zend_string *string_key, bool try_from) |
291 | 192 | { |
292 | 192 | if (ce->type == ZEND_USER_CLASS && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) { |
293 | 92 | if (zend_update_class_constants(ce) == FAILURE) { |
294 | 31 | return FAILURE; |
295 | 31 | } |
296 | 92 | } |
297 | | |
298 | 161 | const HashTable *backed_enum_table = CE_BACKED_ENUM_TABLE(ce); |
299 | 161 | if (!backed_enum_table) { |
300 | 12 | goto not_found; |
301 | 12 | } |
302 | | |
303 | 149 | zval *case_name_zv; |
304 | 149 | if (ce->enum_backing_type == IS_LONG) { |
305 | 53 | case_name_zv = zend_hash_index_find(backed_enum_table, long_key); |
306 | 96 | } else { |
307 | 96 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
308 | 96 | ZEND_ASSERT(string_key != NULL); |
309 | 96 | case_name_zv = zend_hash_find(backed_enum_table, string_key); |
310 | 96 | } |
311 | | |
312 | 149 | if (case_name_zv == NULL) { |
313 | 44 | not_found: |
314 | 44 | if (try_from) { |
315 | 16 | *result = NULL; |
316 | 16 | return SUCCESS; |
317 | 16 | } |
318 | | |
319 | 28 | if (ce->enum_backing_type == IS_LONG) { |
320 | 6 | zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum %s", long_key, ZSTR_VAL(ce->name)); |
321 | 22 | } else { |
322 | 22 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
323 | 22 | zend_value_error("\"%s\" is not a valid backing value for enum %s", ZSTR_VAL(string_key), ZSTR_VAL(ce->name)); |
324 | 22 | } |
325 | 28 | return FAILURE; |
326 | 28 | } |
327 | | |
328 | | // TODO: We might want to store pointers to constants in backed_enum_table instead of names, |
329 | | // to make this lookup more efficient. |
330 | 117 | ZEND_ASSERT(Z_TYPE_P(case_name_zv) == IS_STRING); |
331 | 117 | zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), Z_STR_P(case_name_zv)); |
332 | 117 | ZEND_ASSERT(c != NULL); |
333 | 117 | zval *case_zv = &c->value; |
334 | 117 | if (Z_TYPE_P(case_zv) == IS_CONSTANT_AST) { |
335 | 0 | if (zval_update_constant_ex(case_zv, c->ce) == FAILURE) { |
336 | 0 | return FAILURE; |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | 117 | *result = Z_OBJ_P(case_zv); |
341 | 117 | return SUCCESS; |
342 | 117 | } |
343 | | |
344 | | static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try_from) |
345 | 222 | { |
346 | 222 | zend_class_entry *ce = execute_data->func->common.scope; |
347 | 222 | bool release_string = false; |
348 | 222 | zend_string *string_key = NULL; |
349 | 222 | zend_long long_key = 0; |
350 | | |
351 | 222 | if (ce->enum_backing_type == IS_LONG) { |
352 | 306 | ZEND_PARSE_PARAMETERS_START(1, 1) |
353 | 408 | Z_PARAM_LONG(long_key) |
354 | 102 | ZEND_PARSE_PARAMETERS_END(); |
355 | 120 | } else { |
356 | 120 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
357 | | |
358 | 120 | if (ZEND_ARG_USES_STRICT_TYPES()) { |
359 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
360 | 0 | Z_PARAM_STR(string_key) |
361 | 0 | ZEND_PARSE_PARAMETERS_END(); |
362 | 120 | } else { |
363 | | // We allow long keys so that coercion to string doesn't happen implicitly. The JIT |
364 | | // skips deallocation of params that don't require it. In the case of from/tryFrom |
365 | | // passing int to from(int|string) looks like no coercion will happen, so the JIT |
366 | | // won't emit a dtor call. Thus we allocate/free the string manually. |
367 | 360 | ZEND_PARSE_PARAMETERS_START(1, 1) |
368 | 600 | Z_PARAM_STR_OR_LONG(string_key, long_key) |
369 | 600 | ZEND_PARSE_PARAMETERS_END(); |
370 | | |
371 | 120 | if (string_key == NULL) { |
372 | 13 | release_string = true; |
373 | 13 | string_key = zend_long_to_str(long_key); |
374 | 13 | } |
375 | 120 | } |
376 | 120 | } |
377 | | |
378 | 192 | zend_object *case_obj; |
379 | 192 | if (zend_enum_get_case_by_value(&case_obj, ce, long_key, string_key, try_from) == FAILURE) { |
380 | 59 | goto throw; |
381 | 59 | } |
382 | | |
383 | 133 | if (case_obj == NULL) { |
384 | 16 | ZEND_ASSERT(try_from); |
385 | 16 | goto return_null; |
386 | 16 | } |
387 | | |
388 | 117 | if (release_string) { |
389 | 0 | zend_string_release(string_key); |
390 | 0 | } |
391 | 117 | RETURN_OBJ_COPY(case_obj); |
392 | | |
393 | 59 | throw: |
394 | 59 | if (release_string) { |
395 | 13 | zend_string_release(string_key); |
396 | 13 | } |
397 | 59 | RETURN_THROWS(); |
398 | | |
399 | 16 | return_null: |
400 | 16 | if (release_string) { |
401 | 0 | zend_string_release(string_key); |
402 | 0 | } |
403 | 16 | RETURN_NULL(); |
404 | 16 | } |
405 | | |
406 | | static ZEND_NAMED_FUNCTION(zend_enum_from_func) |
407 | 137 | { |
408 | 137 | zend_enum_from_base(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); |
409 | 137 | } |
410 | | |
411 | | static ZEND_NAMED_FUNCTION(zend_enum_try_from_func) |
412 | 85 | { |
413 | 85 | zend_enum_from_base(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); |
414 | 85 | } |
415 | | |
416 | 2.45k | static void zend_enum_register_func(zend_class_entry *ce, zend_known_string_id name_id, zend_internal_function *zif) { |
417 | 2.45k | zend_string *name = ZSTR_KNOWN(name_id); |
418 | 2.45k | zif->type = ZEND_INTERNAL_FUNCTION; |
419 | 2.45k | zif->module = EG(current_module); |
420 | 2.45k | zif->scope = ce; |
421 | 2.45k | zif->T = ZEND_OBSERVER_ENABLED; |
422 | 2.45k | if (EG(active)) { // at run-time |
423 | 2.45k | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
424 | 0 | zif->fn_flags |= ZEND_ACC_PRELOADED; |
425 | 0 | } |
426 | 2.45k | ZEND_MAP_PTR_INIT(zif->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size())); |
427 | 2.45k | } else { |
428 | | #ifdef ZTS |
429 | | ZEND_MAP_PTR_NEW_STATIC(zif->run_time_cache); |
430 | | #else |
431 | 0 | ZEND_MAP_PTR_INIT(zif->run_time_cache, NULL); |
432 | 0 | #endif |
433 | 0 | } |
434 | | |
435 | 2.45k | if (!zend_hash_add_ptr(&ce->function_table, name, zif)) { |
436 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
437 | 11 | } |
438 | 2.45k | } |
439 | | |
440 | | void zend_enum_register_funcs(zend_class_entry *ce) |
441 | 1.47k | { |
442 | 1.47k | const uint32_t fn_flags = |
443 | 1.47k | ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_ARENA_ALLOCATED; |
444 | 1.47k | zend_internal_function *cases_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
445 | 1.47k | cases_function->handler = zend_enum_cases_func; |
446 | 1.47k | cases_function->function_name = ZSTR_KNOWN(ZEND_STR_CASES); |
447 | 1.47k | cases_function->fn_flags = fn_flags; |
448 | 1.47k | cases_function->doc_comment = NULL; |
449 | 1.47k | cases_function->arg_info = (zend_internal_arg_info *) (arginfo_class_UnitEnum_cases + 1); |
450 | 1.47k | zend_enum_register_func(ce, ZEND_STR_CASES, cases_function); |
451 | | |
452 | 1.47k | if (ce->enum_backing_type != IS_UNDEF) { |
453 | 490 | zend_internal_function *from_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
454 | 490 | from_function->handler = zend_enum_from_func; |
455 | 490 | from_function->function_name = ZSTR_KNOWN(ZEND_STR_FROM); |
456 | 490 | from_function->fn_flags = fn_flags; |
457 | 490 | from_function->doc_comment = NULL; |
458 | 490 | from_function->num_args = 1; |
459 | 490 | from_function->required_num_args = 1; |
460 | 490 | from_function->arg_info = (zend_internal_arg_info *) (arginfo_class_BackedEnum_from + 1); |
461 | 490 | zend_enum_register_func(ce, ZEND_STR_FROM, from_function); |
462 | | |
463 | 490 | zend_internal_function *try_from_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
464 | 490 | try_from_function->handler = zend_enum_try_from_func; |
465 | 490 | try_from_function->function_name = ZSTR_KNOWN(ZEND_STR_TRYFROM); |
466 | 490 | try_from_function->fn_flags = fn_flags; |
467 | 490 | try_from_function->doc_comment = NULL; |
468 | 490 | try_from_function->num_args = 1; |
469 | 490 | try_from_function->required_num_args = 1; |
470 | 490 | try_from_function->arg_info = (zend_internal_arg_info *) (arginfo_class_BackedEnum_tryFrom + 1); |
471 | 490 | zend_enum_register_func(ce, ZEND_STR_TRYFROM_LOWERCASE, try_from_function); |
472 | 490 | } |
473 | 1.47k | } |
474 | | |
475 | | void zend_enum_register_props(zend_class_entry *ce) |
476 | 5.92k | { |
477 | 5.92k | ce->ce_flags |= ZEND_ACC_NO_DYNAMIC_PROPERTIES; |
478 | | |
479 | 5.92k | zval name_default_value; |
480 | 5.92k | ZVAL_UNDEF(&name_default_value); |
481 | 5.92k | zend_type name_type = ZEND_TYPE_INIT_CODE(IS_STRING, 0, 0); |
482 | 5.92k | zend_declare_typed_property(ce, ZSTR_KNOWN(ZEND_STR_NAME), &name_default_value, ZEND_ACC_PUBLIC | ZEND_ACC_READONLY, NULL, name_type); |
483 | | |
484 | 5.92k | if (ce->enum_backing_type != IS_UNDEF) { |
485 | 748 | zval value_default_value; |
486 | 748 | ZVAL_UNDEF(&value_default_value); |
487 | 748 | zend_type value_type = ZEND_TYPE_INIT_CODE(ce->enum_backing_type, 0, 0); |
488 | 748 | zend_declare_typed_property(ce, ZSTR_KNOWN(ZEND_STR_VALUE), &value_default_value, ZEND_ACC_PUBLIC | ZEND_ACC_READONLY, NULL, value_type); |
489 | 748 | } |
490 | 5.92k | } |
491 | | |
492 | | static const zend_function_entry unit_enum_methods[] = { |
493 | | ZEND_NAMED_ME(cases, zend_enum_cases_func, arginfo_class_UnitEnum_cases, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) |
494 | | ZEND_FE_END |
495 | | }; |
496 | | |
497 | | static const zend_function_entry backed_enum_methods[] = { |
498 | | ZEND_NAMED_ME(cases, zend_enum_cases_func, arginfo_class_UnitEnum_cases, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) |
499 | | ZEND_NAMED_ME(from, zend_enum_from_func, arginfo_class_BackedEnum_from, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) |
500 | | ZEND_NAMED_ME(tryFrom, zend_enum_try_from_func, arginfo_class_BackedEnum_tryFrom, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) |
501 | | ZEND_FE_END |
502 | | }; |
503 | | |
504 | | ZEND_API zend_class_entry *zend_register_internal_enum( |
505 | | const char *name, uint8_t type, const zend_function_entry *functions) |
506 | 80 | { |
507 | 80 | ZEND_ASSERT(type == IS_UNDEF || type == IS_LONG || type == IS_STRING); |
508 | | |
509 | 80 | zend_class_entry tmp_ce; |
510 | 80 | INIT_CLASS_ENTRY_EX(tmp_ce, name, strlen(name), functions); |
511 | | |
512 | 80 | zend_class_entry *ce = zend_register_internal_class(&tmp_ce); |
513 | 80 | ce->ce_flags |= ZEND_ACC_ENUM; |
514 | 80 | ce->enum_backing_type = type; |
515 | 80 | if (type != IS_UNDEF) { |
516 | 16 | HashTable *backed_enum_table = pemalloc(sizeof(HashTable), 1); |
517 | 16 | zend_hash_init(backed_enum_table, 0, NULL, ZVAL_PTR_DTOR, 1); |
518 | 16 | zend_class_set_backed_enum_table(ce, backed_enum_table); |
519 | 16 | } |
520 | | |
521 | 80 | zend_enum_register_props(ce); |
522 | 80 | if (type == IS_UNDEF) { |
523 | 64 | zend_register_functions( |
524 | 64 | ce, unit_enum_methods, &ce->function_table, EG(current_module)->type); |
525 | 64 | zend_class_implements(ce, 1, zend_ce_unit_enum); |
526 | 64 | } else { |
527 | 16 | zend_register_functions( |
528 | 16 | ce, backed_enum_methods, &ce->function_table, EG(current_module)->type); |
529 | 16 | zend_class_implements(ce, 1, zend_ce_backed_enum); |
530 | 16 | } |
531 | | |
532 | 80 | return ce; |
533 | 80 | } |
534 | | |
535 | | static zend_ast_ref *create_enum_case_ast( |
536 | 720 | zend_string *class_name, zend_string *case_name, zval *value) { |
537 | | // TODO: Use custom node type for enum cases? |
538 | 720 | size_t size = sizeof(zend_ast_ref) + zend_ast_size(3) |
539 | 720 | + (value ? 3 : 2) * sizeof(zend_ast_zval); |
540 | 720 | char *p = pemalloc(size, 1); |
541 | 720 | zend_ast_ref *ref = (zend_ast_ref *) p; p += sizeof(zend_ast_ref); |
542 | 720 | GC_SET_REFCOUNT(ref, 1); |
543 | 720 | GC_TYPE_INFO(ref) = GC_CONSTANT_AST | GC_PERSISTENT | GC_IMMUTABLE; |
544 | | |
545 | 720 | zend_ast *ast = (zend_ast *) p; p += zend_ast_size(3); |
546 | 720 | ast->kind = ZEND_AST_CONST_ENUM_INIT; |
547 | 720 | ast->attr = 0; |
548 | 720 | ast->lineno = 0; |
549 | | |
550 | 720 | ast->child[0] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
551 | 720 | ast->child[0]->kind = ZEND_AST_ZVAL; |
552 | 720 | ast->child[0]->attr = 0; |
553 | 720 | ZEND_ASSERT(ZSTR_IS_INTERNED(class_name)); |
554 | 720 | ZVAL_STR(zend_ast_get_zval(ast->child[0]), class_name); |
555 | 720 | Z_LINENO_P(zend_ast_get_zval(ast->child[0])) = 0; |
556 | | |
557 | 720 | ast->child[1] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
558 | 720 | ast->child[1]->kind = ZEND_AST_ZVAL; |
559 | 720 | ast->child[1]->attr = 0; |
560 | 720 | ZEND_ASSERT(ZSTR_IS_INTERNED(case_name)); |
561 | 720 | ZVAL_STR(zend_ast_get_zval(ast->child[1]), case_name); |
562 | 720 | Z_LINENO_P(zend_ast_get_zval(ast->child[1])) = 0; |
563 | | |
564 | 720 | if (value) { |
565 | 32 | ast->child[2] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
566 | 32 | ast->child[2]->kind = ZEND_AST_ZVAL; |
567 | 32 | ast->child[2]->attr = 0; |
568 | 32 | ZEND_ASSERT(!Z_REFCOUNTED_P(value)); |
569 | 32 | ZVAL_COPY_VALUE(zend_ast_get_zval(ast->child[2]), value); |
570 | 32 | Z_LINENO_P(zend_ast_get_zval(ast->child[2])) = 0; |
571 | 688 | } else { |
572 | 688 | ast->child[2] = NULL; |
573 | 688 | } |
574 | | |
575 | 720 | return ref; |
576 | 720 | } |
577 | | |
578 | | ZEND_API void zend_enum_add_case(zend_class_entry *ce, zend_string *case_name, zval *value) |
579 | 720 | { |
580 | 720 | if (value) { |
581 | 32 | ZEND_ASSERT(ce->enum_backing_type == Z_TYPE_P(value)); |
582 | 32 | if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) { |
583 | 32 | zval_make_interned_string(value); |
584 | 32 | } |
585 | | |
586 | 32 | HashTable *backed_enum_table = CE_BACKED_ENUM_TABLE(ce); |
587 | | |
588 | 32 | zval case_name_zv; |
589 | 32 | ZVAL_STR(&case_name_zv, case_name); |
590 | 32 | if (Z_TYPE_P(value) == IS_LONG) { |
591 | 0 | zend_hash_index_add_new(backed_enum_table, Z_LVAL_P(value), &case_name_zv); |
592 | 32 | } else { |
593 | 32 | zend_hash_add_new(backed_enum_table, Z_STR_P(value), &case_name_zv); |
594 | 32 | } |
595 | 688 | } else { |
596 | 688 | ZEND_ASSERT(ce->enum_backing_type == IS_UNDEF); |
597 | 688 | } |
598 | | |
599 | 720 | zval ast_zv; |
600 | 720 | Z_TYPE_INFO(ast_zv) = IS_CONSTANT_AST; |
601 | 720 | Z_AST(ast_zv) = create_enum_case_ast(ce->name, case_name, value); |
602 | 720 | zend_class_constant *c = zend_declare_class_constant_ex( |
603 | 720 | ce, case_name, &ast_zv, ZEND_ACC_PUBLIC, NULL); |
604 | 720 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE; |
605 | 720 | } |
606 | | |
607 | | ZEND_API void zend_enum_add_case_cstr(zend_class_entry *ce, const char *name, zval *value) |
608 | 720 | { |
609 | 720 | zend_string *name_str = zend_string_init_interned(name, strlen(name), 1); |
610 | 720 | zend_enum_add_case(ce, name_str, value); |
611 | 720 | zend_string_release(name_str); |
612 | 720 | } |
613 | | |
614 | 0 | static zend_object *zend_enum_case_from_class_constant(zend_class_constant *c) { |
615 | 0 | ZEND_ASSERT(c && "Must be a valid enum case"); |
616 | 0 | ZEND_ASSERT(ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE); |
617 | |
|
618 | 0 | if (Z_TYPE(c->value) == IS_CONSTANT_AST) { |
619 | 0 | if (zval_update_constant_ex(&c->value, c->ce) == FAILURE) { |
620 | 0 | ZEND_UNREACHABLE(); |
621 | 0 | } |
622 | 0 | } |
623 | 0 | ZEND_ASSERT(Z_TYPE(c->value) == IS_OBJECT); |
624 | 0 | return Z_OBJ(c->value); |
625 | 0 | } |
626 | | |
627 | 0 | ZEND_API zend_object *zend_enum_get_case(zend_class_entry *ce, zend_string *name) { |
628 | 0 | zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), name); |
629 | 0 | return zend_enum_case_from_class_constant(c); |
630 | 0 | } |
631 | | |
632 | 0 | ZEND_API zend_object *zend_enum_get_case_cstr(zend_class_entry *ce, const char *name) { |
633 | 0 | zend_class_constant *c = zend_hash_str_find_ptr(CE_CONSTANTS_TABLE(ce), name, strlen(name)); |
634 | 0 | return zend_enum_case_from_class_constant(c); |
635 | 0 | } |