1use bitflags::bitflags;
4
5#[cfg(php81)]
6use crate::ffi::ZEND_ACC_ENUM;
7#[cfg(not(php82))]
8use crate::ffi::ZEND_ACC_REUSE_GET_ITERATOR;
9use crate::ffi::{
10 _IS_BOOL, CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, E_COMPILE_ERROR,
11 E_COMPILE_WARNING, E_CORE_ERROR, E_CORE_WARNING, E_DEPRECATED, E_ERROR, E_NOTICE, E_PARSE,
12 E_RECOVERABLE_ERROR, E_STRICT, E_USER_DEPRECATED, E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING,
13 E_WARNING, GC_IMMUTABLE, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE,
14 IS_INDIRECT, IS_ITERABLE, IS_LONG, IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE,
15 IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID,
16 PHP_INI_ALL, PHP_INI_PERDIR, PHP_INI_SYSTEM, PHP_INI_USER, Z_TYPE_FLAGS_SHIFT,
17 ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS, ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED,
18 ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED, ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED,
19 ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING, ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL,
20 ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK, ZEND_ACC_HAS_RETURN_TYPE,
21 ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
22 ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
23 ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
24 ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
25 ZEND_ACC_RESOLVED_PARENT, ZEND_ACC_RETURN_REFERENCE, ZEND_ACC_STATIC, ZEND_ACC_STRICT_TYPES,
26 ZEND_ACC_TOP_LEVEL, ZEND_ACC_TRAIT, ZEND_ACC_TRAIT_CLONE, ZEND_ACC_UNRESOLVED_VARIANCE,
27 ZEND_ACC_USE_GUARDS, ZEND_ACC_USES_THIS, ZEND_ACC_VARIADIC, ZEND_EVAL_CODE,
28 ZEND_HAS_STATIC_IN_METHODS, ZEND_INTERNAL_FUNCTION, ZEND_USER_FUNCTION,
29};
30
31use std::{convert::TryFrom, fmt::Display};
32
33use crate::error::{Error, Result};
34
35bitflags! {
36 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
38 pub struct ZvalTypeFlags: u32 {
39 const Undef = IS_UNDEF;
41 const Null = IS_NULL;
43 const False = IS_FALSE;
45 const True = IS_TRUE;
47 const Long = IS_LONG;
49 const Double = IS_DOUBLE;
51 const String = IS_STRING;
53 const Array = IS_ARRAY;
55 const Object = IS_OBJECT;
57 const Resource = IS_RESOURCE;
59 const Reference = IS_REFERENCE;
61 const Callable = IS_CALLABLE;
63 const ConstantExpression = IS_CONSTANT_AST;
65 const Void = IS_VOID;
67 const Ptr = IS_PTR;
69 const Iterable = IS_ITERABLE;
71
72 const InternedStringEx = Self::String.bits();
74 const StringEx = Self::String.bits() | Self::RefCounted.bits();
76 const ArrayEx = Self::Array.bits() | Self::RefCounted.bits() | Self::Collectable.bits();
78 const ObjectEx = Self::Object.bits() | Self::RefCounted.bits() | Self::Collectable.bits();
80 const ResourceEx = Self::Resource.bits() | Self::RefCounted.bits();
82 const ReferenceEx = Self::Reference.bits() | Self::RefCounted.bits();
84 const ConstantAstEx = Self::ConstantExpression.bits() | Self::RefCounted.bits();
86
87 const RefCounted = (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT);
89 const Collectable = (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT);
91
92 const Immutable = GC_IMMUTABLE;
94 }
95}
96
97bitflags! {
98 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
100 pub struct ClassFlags: u32 {
101 const Final = ZEND_ACC_FINAL;
103 const Abstract = ZEND_ACC_ABSTRACT;
105 const Immutable = ZEND_ACC_IMMUTABLE;
108 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
110 const TopLevel = ZEND_ACC_TOP_LEVEL;
112 const Preloaded = ZEND_ACC_PRELOADED;
114
115 const Interface = ZEND_ACC_INTERFACE;
117 const Trait = ZEND_ACC_TRAIT;
119 const AnonymousClass = ZEND_ACC_ANON_CLASS;
121 #[cfg(php81)]
123 const Enum = ZEND_ACC_ENUM;
124 const Linked = ZEND_ACC_LINKED;
126 const ImplicitAbstractClass = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
128 const UseGuards = ZEND_ACC_USE_GUARDS;
130
131 const ConstantsUpdated = ZEND_ACC_CONSTANTS_UPDATED;
133 const NoDynamicProperties = ZEND_ACC_NO_DYNAMIC_PROPERTIES;
135 const HasStaticInMethods = ZEND_HAS_STATIC_IN_METHODS;
137 #[cfg(not(php82))]
139 const ReuseGetIterator = ZEND_ACC_REUSE_GET_ITERATOR;
140 const ResolvedParent = ZEND_ACC_RESOLVED_PARENT;
142 const ResolvedInterfaces = ZEND_ACC_RESOLVED_INTERFACES;
144 const UnresolvedVariance = ZEND_ACC_UNRESOLVED_VARIANCE;
146 const NearlyLinked = ZEND_ACC_NEARLY_LINKED;
148
149 #[cfg(php81)]
151 const NotSerializable = crate::ffi::ZEND_ACC_NOT_SERIALIZABLE;
152
153 #[cfg(php82)]
156 const ReadonlyClass = crate::ffi::ZEND_ACC_READONLY_CLASS;
157 }
158}
159
160bitflags! {
161 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
163 pub struct MethodFlags: u32 {
164 const Public = ZEND_ACC_PUBLIC;
166 const Protected = ZEND_ACC_PROTECTED;
168 const Private = ZEND_ACC_PRIVATE;
170 const Changed = ZEND_ACC_CHANGED;
172 const Static = ZEND_ACC_STATIC;
174 const Final = ZEND_ACC_FINAL;
176 const Abstract = ZEND_ACC_ABSTRACT;
178 const Immutable = ZEND_ACC_IMMUTABLE;
181 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
183 const TopLevel = ZEND_ACC_TOP_LEVEL;
185 const Preloaded = ZEND_ACC_PRELOADED;
187
188 const Deprecated = ZEND_ACC_DEPRECATED;
190 const ReturnReference = ZEND_ACC_RETURN_REFERENCE;
192 const HasReturnType = ZEND_ACC_HAS_RETURN_TYPE;
194 const Variadic = ZEND_ACC_VARIADIC;
196 const HasFinallyBlock = ZEND_ACC_HAS_FINALLY_BLOCK;
198 const EarlyBinding = ZEND_ACC_EARLY_BINDING;
200 const UsesThis = ZEND_ACC_USES_THIS;
202 const CallViaTrampoline = ZEND_ACC_CALL_VIA_TRAMPOLINE;
208 const NeverCache = ZEND_ACC_NEVER_CACHE;
210 const TraitClone = ZEND_ACC_TRAIT_CLONE;
212 const IsConstructor = ZEND_ACC_CTOR;
214 const Closure = ZEND_ACC_CLOSURE;
216 const FakeClosure = ZEND_ACC_FAKE_CLOSURE;
218 const Generator = ZEND_ACC_GENERATOR;
220 const DonePassTwo = ZEND_ACC_DONE_PASS_TWO;
222 const HeapRTCache = ZEND_ACC_HEAP_RT_CACHE;
224 const StrictTypes = ZEND_ACC_STRICT_TYPES;
226 }
227}
228
229bitflags! {
230 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
232 pub struct PropertyFlags: u32 {
233 const Public = ZEND_ACC_PUBLIC;
235 const Protected = ZEND_ACC_PROTECTED;
237 const Private = ZEND_ACC_PRIVATE;
239 const Changed = ZEND_ACC_CHANGED;
241 const Static = ZEND_ACC_STATIC;
243 const Promoted = ZEND_ACC_PROMOTED;
245 }
246}
247
248bitflags! {
249 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
251 pub struct ConstantFlags: u32 {
252 const Public = ZEND_ACC_PUBLIC;
254 const Protected = ZEND_ACC_PROTECTED;
256 const Private = ZEND_ACC_PRIVATE;
258 const Promoted = ZEND_ACC_PROMOTED;
260 }
261}
262
263bitflags! {
264 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
266 pub struct GlobalConstantFlags: u32 {
267 #[deprecated(note = "No longer used -- always case-sensitive")]
269 const CaseSensitive = CONST_CS;
270 const Persistent = CONST_PERSISTENT;
272 const NoFileCache = CONST_NO_FILE_CACHE;
274 const Deprecated = CONST_DEPRECATED;
276 }
277}
278
279bitflags! {
280 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
282 pub struct ZendResult: i32 {
283 const Success = 0;
285 const Failure = -1;
287 }
288}
289
290bitflags! {
291 pub struct IniEntryPermission: u32 {
293 const User = PHP_INI_USER;
295 const PerDir = PHP_INI_PERDIR;
297 const System = PHP_INI_SYSTEM;
299 const All = PHP_INI_ALL;
301 }
302}
303
304bitflags! {
305 pub struct ErrorType: u32 {
307 const Error = E_ERROR;
309 const Warning = E_WARNING;
311 const Parse = E_PARSE;
313 const Notice = E_NOTICE;
315 const CoreError = E_CORE_ERROR;
317 const CoreWarning = E_CORE_WARNING;
319 const CompileError = E_COMPILE_ERROR;
321 const CompileWarning = E_COMPILE_WARNING;
323 #[cfg_attr(php84, deprecated = "`E_USER_ERROR` is deprecated since PHP 8.4. Throw an exception instead.")]
325 const UserError = E_USER_ERROR;
326 const UserWarning = E_USER_WARNING;
328 const UserNotice = E_USER_NOTICE;
330 const Strict = E_STRICT;
332 const RecoverableError = E_RECOVERABLE_ERROR;
334 const Deprecated = E_DEPRECATED;
336 const UserDeprecated = E_USER_DEPRECATED;
338 }
339}
340
341#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
343pub enum FunctionType {
344 Internal,
346 User,
348 Eval,
350}
351
352impl From<u8> for FunctionType {
353 #[allow(clippy::bad_bit_mask)]
354 fn from(value: u8) -> Self {
355 match value.into() {
356 ZEND_INTERNAL_FUNCTION => Self::Internal,
357 ZEND_USER_FUNCTION => Self::User,
358 ZEND_EVAL_CODE => Self::Eval,
359 _ => panic!("Unknown function type: {value}"),
360 }
361 }
362}
363
364#[repr(C, u8)]
366#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
367pub enum DataType {
368 Undef,
370 Null,
372 False,
374 True,
376 Long,
378 Double,
380 String,
382 Array,
384 Iterable,
386 Object(Option<&'static str>),
388 Resource,
390 Reference,
392 Callable,
394 ConstantExpression,
396 #[default]
398 Void,
399 Mixed,
401 Bool,
403 Ptr,
405 Indirect,
407}
408
409impl DataType {
410 #[must_use]
412 pub const fn as_u32(&self) -> u32 {
413 match self {
414 DataType::Undef => IS_UNDEF,
415 DataType::Null => IS_NULL,
416 DataType::False => IS_FALSE,
417 DataType::True => IS_TRUE,
418 DataType::Long => IS_LONG,
419 DataType::Double => IS_DOUBLE,
420 DataType::String => IS_STRING,
421 DataType::Array => IS_ARRAY,
422 DataType::Object(_) => IS_OBJECT,
423 DataType::Resource | DataType::Reference => IS_RESOURCE,
424 DataType::Indirect => IS_INDIRECT,
425 DataType::Callable => IS_CALLABLE,
426 DataType::ConstantExpression => IS_CONSTANT_AST,
427 DataType::Void => IS_VOID,
428 DataType::Mixed => IS_MIXED,
429 DataType::Bool => _IS_BOOL,
430 DataType::Ptr => IS_PTR,
431 DataType::Iterable => IS_ITERABLE,
432 }
433 }
434}
435
436impl TryFrom<ZvalTypeFlags> for DataType {
448 type Error = Error;
449
450 fn try_from(value: ZvalTypeFlags) -> Result<Self> {
451 macro_rules! contains {
452 ($t: ident) => {
453 if value.contains(ZvalTypeFlags::$t) {
454 return Ok(DataType::$t);
455 }
456 };
457 }
458
459 contains!(Undef);
460 contains!(Null);
461 contains!(False);
462 contains!(True);
463 contains!(False);
464 contains!(Long);
465 contains!(Double);
466 contains!(String);
467 contains!(Array);
468 contains!(Resource);
469 contains!(Callable);
470 contains!(ConstantExpression);
471 contains!(Void);
472
473 if value.contains(ZvalTypeFlags::Object) {
474 return Ok(DataType::Object(None));
475 }
476
477 Err(Error::UnknownDatatype(0))
478 }
479}
480
481impl From<u32> for DataType {
482 #[allow(clippy::bad_bit_mask)]
483 fn from(value: u32) -> Self {
484 macro_rules! contains {
485 ($c: ident, $t: ident) => {
486 if (value & $c) == $c {
487 return DataType::$t;
488 }
489 };
490 }
491
492 contains!(IS_VOID, Void);
493 contains!(IS_PTR, Ptr);
494 contains!(IS_INDIRECT, Indirect);
495 contains!(IS_CALLABLE, Callable);
496 contains!(IS_CONSTANT_AST, ConstantExpression);
497 contains!(IS_REFERENCE, Reference);
498 contains!(IS_RESOURCE, Resource);
499 contains!(IS_ARRAY, Array);
500 contains!(IS_STRING, String);
501 contains!(IS_DOUBLE, Double);
502 contains!(IS_LONG, Long);
503 contains!(IS_TRUE, True);
504 contains!(IS_FALSE, False);
505 contains!(IS_NULL, Null);
506
507 if (value & IS_OBJECT) == IS_OBJECT {
508 return DataType::Object(None);
509 }
510
511 contains!(IS_UNDEF, Undef);
512
513 DataType::Mixed
514 }
515}
516
517impl Display for DataType {
518 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
519 match self {
520 DataType::Undef => write!(f, "Undefined"),
521 DataType::Null => write!(f, "Null"),
522 DataType::False => write!(f, "False"),
523 DataType::True => write!(f, "True"),
524 DataType::Long => write!(f, "Long"),
525 DataType::Double => write!(f, "Double"),
526 DataType::String => write!(f, "String"),
527 DataType::Array => write!(f, "Array"),
528 DataType::Object(obj) => write!(f, "{}", obj.as_deref().unwrap_or("Object")),
529 DataType::Resource => write!(f, "Resource"),
530 DataType::Reference => write!(f, "Reference"),
531 DataType::Callable => write!(f, "Callable"),
532 DataType::ConstantExpression => write!(f, "Constant Expression"),
533 DataType::Void => write!(f, "Void"),
534 DataType::Bool => write!(f, "Bool"),
535 DataType::Mixed => write!(f, "Mixed"),
536 DataType::Ptr => write!(f, "Pointer"),
537 DataType::Indirect => write!(f, "Indirect"),
538 DataType::Iterable => write!(f, "Iterable"),
539 }
540 }
541}
542
543#[cfg(test)]
544mod tests {
545 #![allow(clippy::unnecessary_fallible_conversions)]
546 use super::DataType;
547 use crate::ffi::{
548 IS_ARRAY, IS_ARRAY_EX, IS_CONSTANT_AST, IS_CONSTANT_AST_EX, IS_DOUBLE, IS_FALSE,
549 IS_INDIRECT, IS_INTERNED_STRING_EX, IS_LONG, IS_NULL, IS_OBJECT, IS_OBJECT_EX, IS_PTR,
550 IS_REFERENCE, IS_REFERENCE_EX, IS_RESOURCE, IS_RESOURCE_EX, IS_STRING, IS_STRING_EX,
551 IS_TRUE, IS_UNDEF, IS_VOID,
552 };
553 use std::convert::TryFrom;
554
555 #[test]
556 fn test_datatype() {
557 macro_rules! test {
558 ($c: ident, $t: ident) => {
559 assert_eq!(DataType::try_from($c), Ok(DataType::$t));
560 };
561 }
562
563 test!(IS_UNDEF, Undef);
564 test!(IS_NULL, Null);
565 test!(IS_FALSE, False);
566 test!(IS_TRUE, True);
567 test!(IS_LONG, Long);
568 test!(IS_DOUBLE, Double);
569 test!(IS_STRING, String);
570 test!(IS_ARRAY, Array);
571 assert_eq!(DataType::try_from(IS_OBJECT), Ok(DataType::Object(None)));
572 test!(IS_RESOURCE, Resource);
573 test!(IS_REFERENCE, Reference);
574 test!(IS_CONSTANT_AST, ConstantExpression);
575 test!(IS_INDIRECT, Indirect);
576 test!(IS_VOID, Void);
577 test!(IS_PTR, Ptr);
578
579 test!(IS_INTERNED_STRING_EX, String);
580 test!(IS_STRING_EX, String);
581 test!(IS_ARRAY_EX, Array);
582 assert_eq!(DataType::try_from(IS_OBJECT_EX), Ok(DataType::Object(None)));
583 test!(IS_RESOURCE_EX, Resource);
584 test!(IS_REFERENCE_EX, Reference);
585 test!(IS_CONSTANT_AST_EX, ConstantExpression);
586 }
587}