/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 | 44 | do { \ |
30 | 44 | if (ce->propertyName) { \ |
31 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), methodName); \ |
32 | 0 | } \ |
33 | 44 | } 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 | | static zend_arg_info zarginfo_class_UnitEnum_cases[sizeof(arginfo_class_UnitEnum_cases)/sizeof(zend_internal_arg_info)]; |
40 | | static zend_arg_info zarginfo_class_BackedEnum_from[sizeof(arginfo_class_BackedEnum_from)/sizeof(zend_internal_arg_info)]; |
41 | | static zend_arg_info zarginfo_class_BackedEnum_tryFrom[sizeof(arginfo_class_BackedEnum_tryFrom)/sizeof(zend_internal_arg_info)]; |
42 | | |
43 | | zend_object *zend_enum_new(zval *result, zend_class_entry *ce, zend_string *case_name, zval *backing_value_zv) |
44 | 3 | { |
45 | 3 | zend_object *zobj = zend_objects_new(ce); |
46 | 3 | GC_ADD_FLAGS(zobj, GC_NOT_COLLECTABLE); |
47 | 3 | ZVAL_OBJ(result, zobj); |
48 | | |
49 | 3 | zval *zname = OBJ_PROP_NUM(zobj, 0); |
50 | 3 | ZVAL_STR_COPY(zname, case_name); |
51 | | /* ZVAL_COPY does not set Z_PROP_FLAG, this needs to be cleared to avoid leaving IS_PROP_REINITABLE set */ |
52 | 3 | Z_PROP_FLAG_P(zname) = 0; |
53 | | |
54 | 3 | if (backing_value_zv != NULL) { |
55 | 2 | zval *prop = OBJ_PROP_NUM(zobj, 1); |
56 | | |
57 | 2 | ZVAL_COPY(prop, backing_value_zv); |
58 | | /* ZVAL_COPY does not set Z_PROP_FLAG, this needs to be cleared to avoid leaving IS_PROP_REINITABLE set */ |
59 | 2 | Z_PROP_FLAG_P(prop) = 0; |
60 | 2 | } |
61 | | |
62 | 3 | return zobj; |
63 | 3 | } |
64 | | |
65 | | static void zend_verify_enum_properties(const zend_class_entry *ce) |
66 | 4 | { |
67 | 4 | const zend_property_info *property_info; |
68 | | |
69 | 20 | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, property_info) { |
70 | 20 | if (zend_string_equals(property_info->name, ZSTR_KNOWN(ZEND_STR_NAME))) { |
71 | 4 | continue; |
72 | 4 | } |
73 | 2 | if ( |
74 | 2 | ce->enum_backing_type != IS_UNDEF |
75 | 2 | && zend_string_equals(property_info->name, ZSTR_KNOWN(ZEND_STR_VALUE)) |
76 | 2 | ) { |
77 | 2 | continue; |
78 | 2 | } |
79 | | // FIXME: File/line number for traits? |
80 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", |
81 | 0 | ZSTR_VAL(ce->name)); |
82 | 2 | } ZEND_HASH_FOREACH_END(); |
83 | 4 | } |
84 | | |
85 | | static void zend_verify_enum_magic_methods(const zend_class_entry *ce) |
86 | 4 | { |
87 | | // Only __get, __call and __invoke are allowed |
88 | | |
89 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(constructor, "__construct"); |
90 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(destructor, "__destruct"); |
91 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(clone, "__clone"); |
92 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__get, "__get"); |
93 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__set, "__set"); |
94 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unset, "__unset"); |
95 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__isset, "__isset"); |
96 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__tostring, "__toString"); |
97 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__debugInfo, "__debugInfo"); |
98 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__serialize, "__serialize"); |
99 | 4 | ZEND_ENUM_DISALLOW_MAGIC_METHOD(__unserialize, "__unserialize"); |
100 | | |
101 | 4 | static const char *const forbidden_methods[] = { |
102 | 4 | "__sleep", |
103 | 4 | "__wakeup", |
104 | 4 | "__set_state", |
105 | 4 | }; |
106 | | |
107 | 4 | uint32_t forbidden_methods_length = sizeof(forbidden_methods) / sizeof(forbidden_methods[0]); |
108 | 16 | for (uint32_t i = 0; i < forbidden_methods_length; ++i) { |
109 | 12 | const char *forbidden_method = forbidden_methods[i]; |
110 | | |
111 | 12 | if (zend_hash_str_exists(&ce->function_table, forbidden_method, strlen(forbidden_method))) { |
112 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include magic method %s", ZSTR_VAL(ce->name), forbidden_method); |
113 | 0 | } |
114 | 12 | } |
115 | 4 | } |
116 | | |
117 | | static void zend_verify_enum_interfaces(const zend_class_entry *ce) |
118 | 4 | { |
119 | 4 | if (zend_class_implements_interface(ce, zend_ce_serializable)) { |
120 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
121 | 0 | "Enum %s cannot implement the Serializable interface", ZSTR_VAL(ce->name)); |
122 | 0 | } |
123 | 4 | } |
124 | | |
125 | | void zend_verify_enum(const zend_class_entry *ce) |
126 | 4 | { |
127 | 4 | zend_verify_enum_properties(ce); |
128 | 4 | zend_verify_enum_magic_methods(ce); |
129 | 4 | zend_verify_enum_interfaces(ce); |
130 | 4 | } |
131 | | |
132 | | static int zend_implement_unit_enum(zend_class_entry *interface, zend_class_entry *class_type) |
133 | 14 | { |
134 | 14 | if (class_type->ce_flags & ZEND_ACC_ENUM) { |
135 | 14 | return SUCCESS; |
136 | 14 | } |
137 | | |
138 | 0 | zend_error_noreturn(E_ERROR, "Non-enum class %s cannot implement interface %s", |
139 | 0 | ZSTR_VAL(class_type->name), |
140 | 0 | ZSTR_VAL(interface->name)); |
141 | | |
142 | 0 | return FAILURE; |
143 | 14 | } |
144 | | |
145 | | static int zend_implement_backed_enum(zend_class_entry *interface, zend_class_entry *class_type) |
146 | 4 | { |
147 | 4 | if (!(class_type->ce_flags & ZEND_ACC_ENUM)) { |
148 | 0 | zend_error_noreturn(E_ERROR, "Non-enum class %s cannot implement interface %s", |
149 | 0 | ZSTR_VAL(class_type->name), |
150 | 0 | ZSTR_VAL(interface->name)); |
151 | 0 | return FAILURE; |
152 | 0 | } |
153 | | |
154 | 4 | if (class_type->enum_backing_type == IS_UNDEF) { |
155 | 0 | zend_error_noreturn(E_ERROR, "Non-backed enum %s cannot implement interface %s", |
156 | 0 | ZSTR_VAL(class_type->name), |
157 | 0 | ZSTR_VAL(interface->name)); |
158 | 0 | return FAILURE; |
159 | 0 | } |
160 | | |
161 | 4 | return SUCCESS; |
162 | 4 | } |
163 | | |
164 | | void zend_register_enum_ce(void) |
165 | 2 | { |
166 | 2 | zend_ce_unit_enum = register_class_UnitEnum(); |
167 | 2 | zend_ce_unit_enum->interface_gets_implemented = zend_implement_unit_enum; |
168 | | |
169 | 2 | zend_ce_backed_enum = register_class_BackedEnum(zend_ce_unit_enum); |
170 | 2 | zend_ce_backed_enum->interface_gets_implemented = zend_implement_backed_enum; |
171 | | |
172 | 2 | memcpy(&zend_enum_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); |
173 | 2 | zend_enum_object_handlers.clone_obj = NULL; |
174 | 2 | zend_enum_object_handlers.compare = zend_objects_not_comparable; |
175 | 2 | } |
176 | | |
177 | | void zend_enum_add_interfaces(zend_class_entry *ce) |
178 | 4 | { |
179 | 4 | uint32_t num_interfaces_before = ce->num_interfaces; |
180 | | |
181 | 4 | ce->num_interfaces++; |
182 | 4 | if (ce->enum_backing_type != IS_UNDEF) { |
183 | 2 | ce->num_interfaces++; |
184 | 2 | } |
185 | | |
186 | 4 | ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)); |
187 | | |
188 | 4 | ce->interface_names = erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
189 | | |
190 | 4 | ce->interface_names[num_interfaces_before].name = zend_string_copy(zend_ce_unit_enum->name); |
191 | 4 | ce->interface_names[num_interfaces_before].lc_name = ZSTR_INIT_LITERAL("unitenum", 0); |
192 | | |
193 | 4 | if (ce->enum_backing_type != IS_UNDEF) { |
194 | 2 | ce->interface_names[num_interfaces_before + 1].name = zend_string_copy(zend_ce_backed_enum->name); |
195 | 2 | ce->interface_names[num_interfaces_before + 1].lc_name = ZSTR_INIT_LITERAL("backedenum", 0); |
196 | 2 | } |
197 | | |
198 | 4 | ce->default_object_handlers = &zend_enum_object_handlers; |
199 | 4 | } |
200 | | |
201 | | zend_result zend_enum_build_backed_enum_table(zend_class_entry *ce) |
202 | 2 | { |
203 | 2 | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM); |
204 | 2 | ZEND_ASSERT(ce->type == ZEND_USER_CLASS); |
205 | | |
206 | 2 | uint32_t backing_type = ce->enum_backing_type; |
207 | 2 | ZEND_ASSERT(backing_type != IS_UNDEF); |
208 | | |
209 | 2 | HashTable *backed_enum_table = emalloc(sizeof(HashTable)); |
210 | 2 | zend_hash_init(backed_enum_table, 0, NULL, ZVAL_PTR_DTOR, 0); |
211 | 2 | zend_class_set_backed_enum_table(ce, backed_enum_table); |
212 | | |
213 | 2 | const zend_string *enum_class_name = ce->name; |
214 | | |
215 | 2 | zend_string *name; |
216 | 2 | zval *val; |
217 | 8 | ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(CE_CONSTANTS_TABLE(ce), name, val) { |
218 | 8 | zend_class_constant *c = Z_PTR_P(val); |
219 | 8 | if ((ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE) == 0) { |
220 | 0 | continue; |
221 | 0 | } |
222 | | |
223 | 2 | zval *c_value = &c->value; |
224 | 2 | zval *case_name = zend_enum_fetch_case_name(Z_OBJ_P(c_value)); |
225 | 2 | zval *case_value = zend_enum_fetch_case_value(Z_OBJ_P(c_value)); |
226 | | |
227 | 2 | 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 | 2 | if (ce->enum_backing_type == IS_LONG) { |
235 | 1 | zend_long long_key = Z_LVAL_P(case_value); |
236 | 1 | const zval *existing_case_name = zend_hash_index_find(backed_enum_table, long_key); |
237 | 1 | if (existing_case_name) { |
238 | 0 | zend_throw_error(NULL, "Duplicate value in enum %s for cases %s and %s", |
239 | 0 | ZSTR_VAL(enum_class_name), |
240 | 0 | Z_STRVAL_P(existing_case_name), |
241 | 0 | ZSTR_VAL(name)); |
242 | 0 | goto failure; |
243 | 0 | } |
244 | 1 | Z_TRY_ADDREF_P(case_name); |
245 | 1 | zend_hash_index_add_new(backed_enum_table, long_key, case_name); |
246 | 1 | } else { |
247 | 1 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
248 | 1 | zend_string *string_key = Z_STR_P(case_value); |
249 | 1 | const zval *existing_case_name = zend_hash_find(backed_enum_table, string_key); |
250 | 1 | 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 | 1 | Z_TRY_ADDREF_P(case_name); |
258 | 1 | zend_hash_add_new(backed_enum_table, string_key, case_name); |
259 | 1 | } |
260 | 2 | } ZEND_HASH_FOREACH_END(); |
261 | | |
262 | 2 | return SUCCESS; |
263 | | |
264 | 0 | failure: |
265 | 0 | zend_hash_release(backed_enum_table); |
266 | 0 | zend_class_set_backed_enum_table(ce, NULL); |
267 | 0 | return FAILURE; |
268 | 2 | } |
269 | | |
270 | | static ZEND_NAMED_FUNCTION(zend_enum_cases_func) |
271 | 0 | { |
272 | 0 | zend_class_entry *ce = execute_data->func->common.scope; |
273 | 0 | zend_class_constant *c; |
274 | |
|
275 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
276 | | |
277 | 0 | array_init(return_value); |
278 | |
|
279 | 0 | ZEND_HASH_MAP_FOREACH_PTR(CE_CONSTANTS_TABLE(ce), c) { |
280 | 0 | if (!(ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE)) { |
281 | 0 | continue; |
282 | 0 | } |
283 | 0 | zval *zv = &c->value; |
284 | 0 | if (Z_TYPE_P(zv) == IS_CONSTANT_AST) { |
285 | 0 | if (zval_update_constant_ex(zv, c->ce) == FAILURE) { |
286 | 0 | RETURN_THROWS(); |
287 | 0 | } |
288 | 0 | } |
289 | 0 | Z_ADDREF_P(zv); |
290 | 0 | zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), zv); |
291 | 0 | } ZEND_HASH_FOREACH_END(); |
292 | 0 | } |
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 | 0 | { |
296 | 0 | 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 | 0 | const HashTable *backed_enum_table = CE_BACKED_ENUM_TABLE(ce); |
303 | 0 | if (!backed_enum_table) { |
304 | 0 | goto not_found; |
305 | 0 | } |
306 | | |
307 | 0 | zval *case_name_zv; |
308 | 0 | if (ce->enum_backing_type == IS_LONG) { |
309 | 0 | case_name_zv = zend_hash_index_find(backed_enum_table, long_key); |
310 | 0 | } 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 | 0 | if (case_name_zv == NULL) { |
317 | 0 | not_found: |
318 | 0 | if (try_from) { |
319 | 0 | *result = NULL; |
320 | 0 | return SUCCESS; |
321 | 0 | } |
322 | | |
323 | 0 | if (ce->enum_backing_type == IS_LONG) { |
324 | 0 | zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum %s", long_key, ZSTR_VAL(ce->name)); |
325 | 0 | } else { |
326 | 0 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
327 | 0 | zend_value_error("\"%s\" is not a valid backing value for enum %s", ZSTR_VAL(string_key), ZSTR_VAL(ce->name)); |
328 | 0 | } |
329 | 0 | return FAILURE; |
330 | 0 | } |
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 | 0 | ZEND_ASSERT(Z_TYPE_P(case_name_zv) == IS_STRING); |
335 | 0 | zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), Z_STR_P(case_name_zv)); |
336 | 0 | ZEND_ASSERT(c != NULL); |
337 | 0 | zval *case_zv = &c->value; |
338 | 0 | 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 | 0 | *result = Z_OBJ_P(case_zv); |
345 | 0 | return SUCCESS; |
346 | 0 | } |
347 | | |
348 | | static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try_from) |
349 | 0 | { |
350 | 0 | zend_class_entry *ce = execute_data->func->common.scope; |
351 | 0 | bool release_string = false; |
352 | 0 | zend_string *string_key = NULL; |
353 | 0 | zend_long long_key = 0; |
354 | |
|
355 | 0 | if (ce->enum_backing_type == IS_LONG) { |
356 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
357 | 0 | Z_PARAM_LONG(long_key) |
358 | 0 | ZEND_PARSE_PARAMETERS_END(); |
359 | 0 | } else { |
360 | 0 | ZEND_ASSERT(ce->enum_backing_type == IS_STRING); |
361 | |
|
362 | 0 | 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 | 0 | } 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 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
372 | 0 | Z_PARAM_STR_OR_LONG(string_key, long_key) |
373 | 0 | ZEND_PARSE_PARAMETERS_END(); |
374 | | |
375 | 0 | if (string_key == NULL) { |
376 | 0 | release_string = true; |
377 | 0 | string_key = zend_long_to_str(long_key); |
378 | 0 | } |
379 | 0 | } |
380 | 0 | } |
381 | | |
382 | 0 | zend_object *case_obj; |
383 | 0 | if (zend_enum_get_case_by_value(&case_obj, ce, long_key, string_key, try_from) == FAILURE) { |
384 | 0 | goto throw; |
385 | 0 | } |
386 | | |
387 | 0 | if (case_obj == NULL) { |
388 | 0 | ZEND_ASSERT(try_from); |
389 | 0 | goto return_null; |
390 | 0 | } |
391 | | |
392 | 0 | if (release_string) { |
393 | 0 | zend_string_release(string_key); |
394 | 0 | } |
395 | 0 | RETURN_OBJ_COPY(case_obj); |
396 | | |
397 | 0 | throw: |
398 | 0 | if (release_string) { |
399 | 0 | zend_string_release(string_key); |
400 | 0 | } |
401 | 0 | 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 | 0 | { |
412 | 0 | zend_enum_from_base(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); |
413 | 0 | } |
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 | 8 | static void zend_enum_register_func(zend_class_entry *ce, zend_known_string_id name_id, zend_internal_function *zif) { |
421 | 8 | zend_string *name = ZSTR_KNOWN(name_id); |
422 | 8 | zif->type = ZEND_INTERNAL_FUNCTION; |
423 | 8 | zif->module = EG(current_module); |
424 | 8 | zif->scope = ce; |
425 | 8 | zif->T = ZEND_OBSERVER_ENABLED; |
426 | 8 | if (EG(active)) { // at run-time |
427 | 8 | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
428 | 0 | zif->fn_flags |= ZEND_ACC_PRELOADED; |
429 | 0 | } |
430 | 8 | ZEND_MAP_PTR_INIT(zif->run_time_cache, zend_arena_calloc(&CG(arena), 1, zend_internal_run_time_cache_reserved_size())); |
431 | 8 | } 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 | 8 | 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 | 8 | } |
443 | | |
444 | | void zend_enum_register_funcs(zend_class_entry *ce) |
445 | 4 | { |
446 | 4 | const uint32_t fn_flags = |
447 | 4 | ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_ARENA_ALLOCATED; |
448 | 4 | zend_internal_function *cases_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
449 | 4 | cases_function->handler = zend_enum_cases_func; |
450 | 4 | cases_function->function_name = ZSTR_KNOWN(ZEND_STR_CASES); |
451 | 4 | cases_function->fn_flags = fn_flags; |
452 | 4 | cases_function->doc_comment = NULL; |
453 | 4 | cases_function->arg_info = zarginfo_class_UnitEnum_cases + 1; |
454 | 4 | zend_enum_register_func(ce, ZEND_STR_CASES, cases_function); |
455 | | |
456 | 4 | if (ce->enum_backing_type != IS_UNDEF) { |
457 | 2 | zend_internal_function *from_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
458 | 2 | from_function->handler = zend_enum_from_func; |
459 | 2 | from_function->function_name = ZSTR_KNOWN(ZEND_STR_FROM); |
460 | 2 | from_function->fn_flags = fn_flags; |
461 | 2 | from_function->doc_comment = NULL; |
462 | 2 | from_function->num_args = 1; |
463 | 2 | from_function->required_num_args = 1; |
464 | 2 | from_function->arg_info = zarginfo_class_BackedEnum_from + 1; |
465 | 2 | zend_enum_register_func(ce, ZEND_STR_FROM, from_function); |
466 | | |
467 | 2 | zend_internal_function *try_from_function = zend_arena_calloc(&CG(arena), sizeof(zend_internal_function), 1); |
468 | 2 | try_from_function->handler = zend_enum_try_from_func; |
469 | 2 | try_from_function->function_name = ZSTR_KNOWN(ZEND_STR_TRYFROM); |
470 | 2 | try_from_function->fn_flags = fn_flags; |
471 | 2 | try_from_function->doc_comment = NULL; |
472 | 2 | try_from_function->num_args = 1; |
473 | 2 | try_from_function->required_num_args = 1; |
474 | 2 | try_from_function->arg_info = zarginfo_class_BackedEnum_tryFrom + 1; |
475 | 2 | zend_enum_register_func(ce, ZEND_STR_TRYFROM_LOWERCASE, try_from_function); |
476 | 2 | } |
477 | 4 | } |
478 | | |
479 | | void zend_enum_register_props(zend_class_entry *ce) |
480 | 14 | { |
481 | 14 | ce->ce_flags |= ZEND_ACC_NO_DYNAMIC_PROPERTIES; |
482 | | |
483 | 14 | zval name_default_value; |
484 | 14 | ZVAL_UNDEF(&name_default_value); |
485 | 14 | zend_type name_type = ZEND_TYPE_INIT_CODE(IS_STRING, 0, 0); |
486 | 14 | zend_declare_typed_property(ce, ZSTR_KNOWN(ZEND_STR_NAME), &name_default_value, ZEND_ACC_PUBLIC | ZEND_ACC_READONLY, NULL, name_type); |
487 | | |
488 | 14 | if (ce->enum_backing_type != IS_UNDEF) { |
489 | 4 | zval value_default_value; |
490 | 4 | ZVAL_UNDEF(&value_default_value); |
491 | 4 | zend_type value_type = ZEND_TYPE_INIT_CODE(ce->enum_backing_type, 0, 0); |
492 | 4 | zend_declare_typed_property(ce, ZSTR_KNOWN(ZEND_STR_VALUE), &value_default_value, ZEND_ACC_PUBLIC | ZEND_ACC_READONLY, NULL, value_type); |
493 | 4 | } |
494 | 14 | } |
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 | 10 | { |
511 | 10 | ZEND_ASSERT(type == IS_UNDEF || type == IS_LONG || type == IS_STRING); |
512 | | |
513 | 10 | zend_class_entry tmp_ce; |
514 | 10 | INIT_CLASS_ENTRY_EX(tmp_ce, name, strlen(name), functions); |
515 | | |
516 | 10 | zend_class_entry *ce = zend_register_internal_class(&tmp_ce); |
517 | 10 | ce->ce_flags |= ZEND_ACC_ENUM; |
518 | 10 | ce->enum_backing_type = type; |
519 | 10 | if (type != IS_UNDEF) { |
520 | 2 | HashTable *backed_enum_table = pemalloc(sizeof(HashTable), 1); |
521 | 2 | zend_hash_init(backed_enum_table, 0, NULL, ZVAL_PTR_DTOR, 1); |
522 | 2 | zend_class_set_backed_enum_table(ce, backed_enum_table); |
523 | 2 | } |
524 | | |
525 | 10 | zend_enum_register_props(ce); |
526 | 10 | if (type == IS_UNDEF) { |
527 | 8 | zend_register_functions( |
528 | 8 | ce, unit_enum_methods, &ce->function_table, EG(current_module)->type); |
529 | 8 | zend_class_implements(ce, 1, zend_ce_unit_enum); |
530 | 8 | } else { |
531 | 2 | zend_register_functions( |
532 | 2 | ce, backed_enum_methods, &ce->function_table, EG(current_module)->type); |
533 | 2 | zend_class_implements(ce, 1, zend_ce_backed_enum); |
534 | 2 | } |
535 | | |
536 | 10 | ce->default_object_handlers = &zend_enum_object_handlers; |
537 | | |
538 | 10 | return ce; |
539 | 10 | } |
540 | | |
541 | | static zend_ast_ref *create_enum_case_ast( |
542 | 90 | zend_string *class_name, zend_string *case_name, zval *value) { |
543 | | // TODO: Use custom node type for enum cases? |
544 | 90 | size_t size = sizeof(zend_ast_ref) + zend_ast_size(3) |
545 | 90 | + (value ? 3 : 2) * sizeof(zend_ast_zval); |
546 | 90 | char *p = pemalloc(size, 1); |
547 | 90 | zend_ast_ref *ref = (zend_ast_ref *) p; p += sizeof(zend_ast_ref); |
548 | 90 | GC_SET_REFCOUNT(ref, 1); |
549 | 90 | GC_TYPE_INFO(ref) = GC_CONSTANT_AST | GC_PERSISTENT | GC_IMMUTABLE; |
550 | | |
551 | 90 | zend_ast *ast = (zend_ast *) p; p += zend_ast_size(3); |
552 | 90 | ast->kind = ZEND_AST_CONST_ENUM_INIT; |
553 | 90 | ast->attr = 0; |
554 | 90 | ast->lineno = 0; |
555 | | |
556 | 90 | ast->child[0] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
557 | 90 | ast->child[0]->kind = ZEND_AST_ZVAL; |
558 | 90 | ast->child[0]->attr = 0; |
559 | 90 | ZEND_ASSERT(ZSTR_IS_INTERNED(class_name)); |
560 | 90 | ZVAL_STR(zend_ast_get_zval(ast->child[0]), class_name); |
561 | 90 | Z_LINENO_P(zend_ast_get_zval(ast->child[0])) = 0; |
562 | | |
563 | 90 | ast->child[1] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
564 | 90 | ast->child[1]->kind = ZEND_AST_ZVAL; |
565 | 90 | ast->child[1]->attr = 0; |
566 | 90 | ZEND_ASSERT(ZSTR_IS_INTERNED(case_name)); |
567 | 90 | ZVAL_STR(zend_ast_get_zval(ast->child[1]), case_name); |
568 | 90 | Z_LINENO_P(zend_ast_get_zval(ast->child[1])) = 0; |
569 | | |
570 | 90 | if (value) { |
571 | 4 | ast->child[2] = (zend_ast *) p; p += sizeof(zend_ast_zval); |
572 | 4 | ast->child[2]->kind = ZEND_AST_ZVAL; |
573 | 4 | ast->child[2]->attr = 0; |
574 | 4 | ZEND_ASSERT(!Z_REFCOUNTED_P(value)); |
575 | 4 | ZVAL_COPY_VALUE(zend_ast_get_zval(ast->child[2]), value); |
576 | 4 | Z_LINENO_P(zend_ast_get_zval(ast->child[2])) = 0; |
577 | 86 | } else { |
578 | 86 | ast->child[2] = NULL; |
579 | 86 | } |
580 | | |
581 | 90 | return ref; |
582 | 90 | } |
583 | | |
584 | | ZEND_API void zend_enum_add_case(zend_class_entry *ce, zend_string *case_name, zval *value) |
585 | 90 | { |
586 | 90 | if (value) { |
587 | 4 | ZEND_ASSERT(ce->enum_backing_type == Z_TYPE_P(value)); |
588 | 4 | if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) { |
589 | 4 | zval_make_interned_string(value); |
590 | 4 | } |
591 | | |
592 | 4 | HashTable *backed_enum_table = CE_BACKED_ENUM_TABLE(ce); |
593 | | |
594 | 4 | zval case_name_zv; |
595 | 4 | ZVAL_STR(&case_name_zv, case_name); |
596 | 4 | if (Z_TYPE_P(value) == IS_LONG) { |
597 | 0 | zend_hash_index_add_new(backed_enum_table, Z_LVAL_P(value), &case_name_zv); |
598 | 4 | } else { |
599 | 4 | zend_hash_add_new(backed_enum_table, Z_STR_P(value), &case_name_zv); |
600 | 4 | } |
601 | 86 | } else { |
602 | 86 | ZEND_ASSERT(ce->enum_backing_type == IS_UNDEF); |
603 | 86 | } |
604 | | |
605 | 90 | zval ast_zv; |
606 | 90 | Z_TYPE_INFO(ast_zv) = IS_CONSTANT_AST; |
607 | 90 | Z_AST(ast_zv) = create_enum_case_ast(ce->name, case_name, value); |
608 | 90 | zend_class_constant *c = zend_declare_class_constant_ex( |
609 | 90 | ce, case_name, &ast_zv, ZEND_ACC_PUBLIC, NULL); |
610 | 90 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE; |
611 | 90 | } |
612 | | |
613 | | ZEND_API void zend_enum_add_case_cstr(zend_class_entry *ce, const char *name, zval *value) |
614 | 90 | { |
615 | 90 | zend_string *name_str = zend_string_init_interned(name, strlen(name), 1); |
616 | 90 | zend_enum_add_case(ce, name_str, value); |
617 | 90 | zend_string_release(name_str); |
618 | 90 | } |
619 | | |
620 | 0 | static zend_object *zend_enum_case_from_class_constant(zend_class_constant *c) { |
621 | 0 | ZEND_ASSERT(c && "Must be a valid enum case"); |
622 | 0 | ZEND_ASSERT(ZEND_CLASS_CONST_FLAGS(c) & ZEND_CLASS_CONST_IS_CASE); |
623 | |
|
624 | 0 | if (Z_TYPE(c->value) == IS_CONSTANT_AST) { |
625 | 0 | if (zval_update_constant_ex(&c->value, c->ce) == FAILURE) { |
626 | 0 | ZEND_UNREACHABLE(); |
627 | 0 | } |
628 | 0 | } |
629 | 0 | ZEND_ASSERT(Z_TYPE(c->value) == IS_OBJECT); |
630 | 0 | return Z_OBJ(c->value); |
631 | 0 | } |
632 | | |
633 | 0 | ZEND_API zend_object *zend_enum_get_case(zend_class_entry *ce, zend_string *name) { |
634 | 0 | zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), name); |
635 | 0 | return zend_enum_case_from_class_constant(c); |
636 | 0 | } |
637 | | |
638 | 0 | ZEND_API zend_object *zend_enum_get_case_cstr(zend_class_entry *ce, const char *name) { |
639 | 0 | zend_class_constant *c = zend_hash_str_find_ptr(CE_CONSTANTS_TABLE(ce), name, strlen(name)); |
640 | 0 | return zend_enum_case_from_class_constant(c); |
641 | 0 | } |
642 | | |
643 | | void zend_enum_startup(void) |
644 | 2 | { |
645 | 4 | for (size_t i = 0; i < sizeof(zarginfo_class_UnitEnum_cases)/sizeof(zend_arg_info); i++) { |
646 | 2 | zend_convert_internal_arg_info(&zarginfo_class_UnitEnum_cases[i], &arginfo_class_UnitEnum_cases[i], i == 0, true); |
647 | 2 | } |
648 | 6 | for (size_t i = 0; i < sizeof(zarginfo_class_BackedEnum_from)/sizeof(zend_arg_info); i++) { |
649 | 4 | zend_convert_internal_arg_info(&zarginfo_class_BackedEnum_from[i], &arginfo_class_BackedEnum_from[i], i == 0, true); |
650 | 4 | } |
651 | 6 | for (size_t i = 0; i < sizeof(zarginfo_class_BackedEnum_tryFrom)/sizeof(zend_arg_info); i++) { |
652 | | zend_convert_internal_arg_info(&zarginfo_class_BackedEnum_tryFrom[i], &arginfo_class_BackedEnum_tryFrom[i], i == 0, true); |
653 | 4 | } |
654 | 2 | } |