Coverage Report

Created: 2025-12-14 06:09

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