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