Coverage Report

Created: 2026-06-02 06:40

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