1use bitflags::bitflags;
4
5#[cfg(not(php82))]
6use crate::ffi::ZEND_ACC_REUSE_GET_ITERATOR;
7use crate::ffi::{
8 CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, E_COMPILE_ERROR,
9 E_COMPILE_WARNING, E_CORE_ERROR, E_CORE_WARNING, E_DEPRECATED, E_ERROR, E_NOTICE, E_PARSE,
10 E_RECOVERABLE_ERROR, E_STRICT, E_USER_DEPRECATED, E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING,
11 E_WARNING, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_INDIRECT,
12 IS_ITERABLE, IS_LONG, IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE,
13 IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, PHP_INI_ALL,
14 PHP_INI_PERDIR, PHP_INI_SYSTEM, PHP_INI_USER, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS,
15 ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED,
16 ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING,
17 ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK,
18 ZEND_ACC_HAS_RETURN_TYPE, ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
19 ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
20 ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
21 ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
22 ZEND_ACC_RESOLVED_PARENT, ZEND_ACC_RETURN_REFERENCE, ZEND_ACC_STATIC, ZEND_ACC_STRICT_TYPES,
23 ZEND_ACC_TOP_LEVEL, ZEND_ACC_TRAIT, ZEND_ACC_TRAIT_CLONE, ZEND_ACC_UNRESOLVED_VARIANCE,
24 ZEND_ACC_USES_THIS, ZEND_ACC_USE_GUARDS, ZEND_ACC_VARIADIC, ZEND_EVAL_CODE,
25 ZEND_HAS_STATIC_IN_METHODS, ZEND_INTERNAL_FUNCTION, ZEND_USER_FUNCTION, Z_TYPE_FLAGS_SHIFT,
26 _IS_BOOL,
27};
28
29use std::{convert::TryFrom, fmt::Display};
30
31use crate::error::{Error, Result};
32
33bitflags! {
34 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
36 pub struct ZvalTypeFlags: u32 {
37 const Undef = IS_UNDEF;
39 const Null = IS_NULL;
41 const False = IS_FALSE;
43 const True = IS_TRUE;
45 const Long = IS_LONG;
47 const Double = IS_DOUBLE;
49 const String = IS_STRING;
51 const Array = IS_ARRAY;
53 const Object = IS_OBJECT;
55 const Resource = IS_RESOURCE;
57 const Reference = IS_REFERENCE;
59 const Callable = IS_CALLABLE;
61 const ConstantExpression = IS_CONSTANT_AST;
63 const Void = IS_VOID;
65 const Ptr = IS_PTR;
67 const Iterable = IS_ITERABLE;
69
70 const InternedStringEx = Self::String.bits();
72 const StringEx = Self::String.bits() | Self::RefCounted.bits();
74 const ArrayEx = Self::Array.bits() | Self::RefCounted.bits() | Self::Collectable.bits();
76 const ObjectEx = Self::Object.bits() | Self::RefCounted.bits() | Self::Collectable.bits();
78 const ResourceEx = Self::Resource.bits() | Self::RefCounted.bits();
80 const ReferenceEx = Self::Reference.bits() | Self::RefCounted.bits();
82 const ConstantAstEx = Self::ConstantExpression.bits() | Self::RefCounted.bits();
84
85 const RefCounted = (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT);
87 const Collectable = (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT);
89 }
90}
91
92bitflags! {
93 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
95 pub struct ClassFlags: u32 {
96 const Final = ZEND_ACC_FINAL;
98 const Abstract = ZEND_ACC_ABSTRACT;
100 const Immutable = ZEND_ACC_IMMUTABLE;
103 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
105 const TopLevel = ZEND_ACC_TOP_LEVEL;
107 const Preloaded = ZEND_ACC_PRELOADED;
109
110 const Interface = ZEND_ACC_INTERFACE;
112 const Trait = ZEND_ACC_TRAIT;
114 const AnonymousClass = ZEND_ACC_ANON_CLASS;
116 const Linked = ZEND_ACC_LINKED;
118 const ImplicitAbstractClass = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
120 const UseGuards = ZEND_ACC_USE_GUARDS;
122
123 const ConstantsUpdated = ZEND_ACC_CONSTANTS_UPDATED;
125 const NoDynamicProperties = ZEND_ACC_NO_DYNAMIC_PROPERTIES;
127 const HasStaticInMethods = ZEND_HAS_STATIC_IN_METHODS;
129 #[cfg(not(php82))]
131 const ReuseGetIterator = ZEND_ACC_REUSE_GET_ITERATOR;
132 const ResolvedParent = ZEND_ACC_RESOLVED_PARENT;
134 const ResolvedInterfaces = ZEND_ACC_RESOLVED_INTERFACES;
136 const UnresolvedVariance = ZEND_ACC_UNRESOLVED_VARIANCE;
138 const NearlyLinked = ZEND_ACC_NEARLY_LINKED;
140
141 #[cfg(php81)]
143 const NotSerializable = crate::ffi::ZEND_ACC_NOT_SERIALIZABLE;
144 }
145}
146
147bitflags! {
148 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
150 pub struct MethodFlags: u32 {
151 const Public = ZEND_ACC_PUBLIC;
153 const Protected = ZEND_ACC_PROTECTED;
155 const Private = ZEND_ACC_PRIVATE;
157 const Changed = ZEND_ACC_CHANGED;
159 const Static = ZEND_ACC_STATIC;
161 const Final = ZEND_ACC_FINAL;
163 const Abstract = ZEND_ACC_ABSTRACT;
165 const Immutable = ZEND_ACC_IMMUTABLE;
168 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
170 const TopLevel = ZEND_ACC_TOP_LEVEL;
172 const Preloaded = ZEND_ACC_PRELOADED;
174
175 const Deprecated = ZEND_ACC_DEPRECATED;
177 const ReturnReference = ZEND_ACC_RETURN_REFERENCE;
179 const HasReturnType = ZEND_ACC_HAS_RETURN_TYPE;
181 const Variadic = ZEND_ACC_VARIADIC;
183 const HasFinallyBlock = ZEND_ACC_HAS_FINALLY_BLOCK;
185 const EarlyBinding = ZEND_ACC_EARLY_BINDING;
187 const UsesThis = ZEND_ACC_USES_THIS;
189 const CallViaTrampoline = ZEND_ACC_CALL_VIA_TRAMPOLINE;
195 const NeverCache = ZEND_ACC_NEVER_CACHE;
197 const TraitClone = ZEND_ACC_TRAIT_CLONE;
199 const IsConstructor = ZEND_ACC_CTOR;
201 const Closure = ZEND_ACC_CLOSURE;
203 const FakeClosure = ZEND_ACC_FAKE_CLOSURE;
205 const Generator = ZEND_ACC_GENERATOR;
207 const DonePassTwo = ZEND_ACC_DONE_PASS_TWO;
209 const HeapRTCache = ZEND_ACC_HEAP_RT_CACHE;
211 const StrictTypes = ZEND_ACC_STRICT_TYPES;
213 }
214}
215
216bitflags! {
217 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
219 pub struct PropertyFlags: u32 {
220 const Public = ZEND_ACC_PUBLIC;
222 const Protected = ZEND_ACC_PROTECTED;
224 const Private = ZEND_ACC_PRIVATE;
226 const Changed = ZEND_ACC_CHANGED;
228 const Static = ZEND_ACC_STATIC;
230 const Promoted = ZEND_ACC_PROMOTED;
232 }
233}
234
235bitflags! {
236 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
238 pub struct ConstantFlags: u32 {
239 const Public = ZEND_ACC_PUBLIC;
241 const Protected = ZEND_ACC_PROTECTED;
243 const Private = ZEND_ACC_PRIVATE;
245 const Promoted = ZEND_ACC_PROMOTED;
247 }
248}
249
250bitflags! {
251 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
253 pub struct GlobalConstantFlags: u32 {
254 #[deprecated(note = "No longer used -- always case-sensitive")]
256 const CaseSensitive = CONST_CS;
257 const Persistent = CONST_PERSISTENT;
259 const NoFileCache = CONST_NO_FILE_CACHE;
261 const Deprecated = CONST_DEPRECATED;
263 }
264}
265
266bitflags! {
267 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
269 pub struct ZendResult: i32 {
270 const Success = 0;
272 const Failure = -1;
274 }
275}
276
277bitflags! {
278 pub struct IniEntryPermission: u32 {
280 const User = PHP_INI_USER;
282 const PerDir = PHP_INI_PERDIR;
284 const System = PHP_INI_SYSTEM;
286 const All = PHP_INI_ALL;
288 }
289}
290
291bitflags! {
292 pub struct ErrorType: u32 {
294 const Error = E_ERROR;
296 const Warning = E_WARNING;
298 const Parse = E_PARSE;
300 const Notice = E_NOTICE;
302 const CoreError = E_CORE_ERROR;
304 const CoreWarning = E_CORE_WARNING;
306 const CompileError = E_COMPILE_ERROR;
308 const CompileWarning = E_COMPILE_WARNING;
310 const UserError = E_USER_ERROR;
312 const UserWarning = E_USER_WARNING;
314 const UserNotice = E_USER_NOTICE;
316 const Strict = E_STRICT;
318 const RecoverableError = E_RECOVERABLE_ERROR;
320 const Deprecated = E_DEPRECATED;
322 const UserDeprecated = E_USER_DEPRECATED;
324 }
325}
326
327#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
329pub enum FunctionType {
330 Internal,
332 User,
334 Eval,
336}
337
338impl From<u8> for FunctionType {
339 #[allow(clippy::bad_bit_mask)]
340 fn from(value: u8) -> Self {
341 match value.into() {
342 ZEND_INTERNAL_FUNCTION => Self::Internal,
343 ZEND_USER_FUNCTION => Self::User,
344 ZEND_EVAL_CODE => Self::Eval,
345 _ => panic!("Unknown function type: {value}"),
346 }
347 }
348}
349
350#[repr(C, u8)]
352#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
353pub enum DataType {
354 Undef,
356 Null,
358 False,
360 True,
362 Long,
364 Double,
366 String,
368 Array,
370 Iterable,
372 Object(Option<&'static str>),
374 Resource,
376 Reference,
378 Callable,
380 ConstantExpression,
382 Void,
384 Mixed,
386 Bool,
388 Ptr,
390 Indirect,
392}
393
394impl Default for DataType {
395 fn default() -> Self {
396 Self::Void
397 }
398}
399
400impl DataType {
401 #[must_use]
403 pub const fn as_u32(&self) -> u32 {
404 match self {
405 DataType::Undef => IS_UNDEF,
406 DataType::Null => IS_NULL,
407 DataType::False => IS_FALSE,
408 DataType::True => IS_TRUE,
409 DataType::Long => IS_LONG,
410 DataType::Double => IS_DOUBLE,
411 DataType::String => IS_STRING,
412 DataType::Array => IS_ARRAY,
413 DataType::Object(_) => IS_OBJECT,
414 DataType::Resource | DataType::Reference => IS_RESOURCE,
415 DataType::Indirect => IS_INDIRECT,
416 DataType::Callable => IS_CALLABLE,
417 DataType::ConstantExpression => IS_CONSTANT_AST,
418 DataType::Void => IS_VOID,
419 DataType::Mixed => IS_MIXED,
420 DataType::Bool => _IS_BOOL,
421 DataType::Ptr => IS_PTR,
422 DataType::Iterable => IS_ITERABLE,
423 }
424 }
425}
426
427impl TryFrom<ZvalTypeFlags> for DataType {
439 type Error = Error;
440
441 fn try_from(value: ZvalTypeFlags) -> Result<Self> {
442 macro_rules! contains {
443 ($t: ident) => {
444 if value.contains(ZvalTypeFlags::$t) {
445 return Ok(DataType::$t);
446 }
447 };
448 }
449
450 contains!(Undef);
451 contains!(Null);
452 contains!(False);
453 contains!(True);
454 contains!(False);
455 contains!(Long);
456 contains!(Double);
457 contains!(String);
458 contains!(Array);
459 contains!(Resource);
460 contains!(Callable);
461 contains!(ConstantExpression);
462 contains!(Void);
463
464 if value.contains(ZvalTypeFlags::Object) {
465 return Ok(DataType::Object(None));
466 }
467
468 Err(Error::UnknownDatatype(0))
469 }
470}
471
472impl From<u32> for DataType {
473 #[allow(clippy::bad_bit_mask)]
474 fn from(value: u32) -> Self {
475 macro_rules! contains {
476 ($c: ident, $t: ident) => {
477 if (value & $c) == $c {
478 return DataType::$t;
479 }
480 };
481 }
482
483 contains!(IS_VOID, Void);
484 contains!(IS_PTR, Ptr);
485 contains!(IS_INDIRECT, Indirect);
486 contains!(IS_CALLABLE, Callable);
487 contains!(IS_CONSTANT_AST, ConstantExpression);
488 contains!(IS_REFERENCE, Reference);
489 contains!(IS_RESOURCE, Resource);
490 contains!(IS_ARRAY, Array);
491 contains!(IS_STRING, String);
492 contains!(IS_DOUBLE, Double);
493 contains!(IS_LONG, Long);
494 contains!(IS_TRUE, True);
495 contains!(IS_FALSE, False);
496 contains!(IS_NULL, Null);
497
498 if (value & IS_OBJECT) == IS_OBJECT {
499 return DataType::Object(None);
500 }
501
502 contains!(IS_UNDEF, Undef);
503
504 DataType::Mixed
505 }
506}
507
508impl Display for DataType {
509 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510 match self {
511 DataType::Undef => write!(f, "Undefined"),
512 DataType::Null => write!(f, "Null"),
513 DataType::False => write!(f, "False"),
514 DataType::True => write!(f, "True"),
515 DataType::Long => write!(f, "Long"),
516 DataType::Double => write!(f, "Double"),
517 DataType::String => write!(f, "String"),
518 DataType::Array => write!(f, "Array"),
519 DataType::Object(obj) => write!(f, "{}", obj.as_deref().unwrap_or("Object")),
520 DataType::Resource => write!(f, "Resource"),
521 DataType::Reference => write!(f, "Reference"),
522 DataType::Callable => write!(f, "Callable"),
523 DataType::ConstantExpression => write!(f, "Constant Expression"),
524 DataType::Void => write!(f, "Void"),
525 DataType::Bool => write!(f, "Bool"),
526 DataType::Mixed => write!(f, "Mixed"),
527 DataType::Ptr => write!(f, "Pointer"),
528 DataType::Indirect => write!(f, "Indirect"),
529 DataType::Iterable => write!(f, "Iterable"),
530 }
531 }
532}
533
534#[cfg(test)]
535mod tests {
536 #![allow(clippy::unnecessary_fallible_conversions)]
537 use super::DataType;
538 use crate::ffi::{
539 IS_ARRAY, IS_ARRAY_EX, IS_CONSTANT_AST, IS_CONSTANT_AST_EX, IS_DOUBLE, IS_FALSE,
540 IS_INDIRECT, IS_INTERNED_STRING_EX, IS_LONG, IS_NULL, IS_OBJECT, IS_OBJECT_EX, IS_PTR,
541 IS_REFERENCE, IS_REFERENCE_EX, IS_RESOURCE, IS_RESOURCE_EX, IS_STRING, IS_STRING_EX,
542 IS_TRUE, IS_UNDEF, IS_VOID,
543 };
544 use std::convert::TryFrom;
545
546 #[test]
547 fn test_datatype() {
548 macro_rules! test {
549 ($c: ident, $t: ident) => {
550 assert_eq!(DataType::try_from($c), Ok(DataType::$t));
551 };
552 }
553
554 test!(IS_UNDEF, Undef);
555 test!(IS_NULL, Null);
556 test!(IS_FALSE, False);
557 test!(IS_TRUE, True);
558 test!(IS_LONG, Long);
559 test!(IS_DOUBLE, Double);
560 test!(IS_STRING, String);
561 test!(IS_ARRAY, Array);
562 assert_eq!(DataType::try_from(IS_OBJECT), Ok(DataType::Object(None)));
563 test!(IS_RESOURCE, Resource);
564 test!(IS_REFERENCE, Reference);
565 test!(IS_CONSTANT_AST, ConstantExpression);
566 test!(IS_INDIRECT, Indirect);
567 test!(IS_VOID, Void);
568 test!(IS_PTR, Ptr);
569
570 test!(IS_INTERNED_STRING_EX, String);
571 test!(IS_STRING_EX, String);
572 test!(IS_ARRAY_EX, Array);
573 assert_eq!(DataType::try_from(IS_OBJECT_EX), Ok(DataType::Object(None)));
574 test!(IS_RESOURCE_EX, Resource);
575 test!(IS_REFERENCE_EX, Reference);
576 test!(IS_CONSTANT_AST_EX, ConstantExpression);
577 }
578}