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 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, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_INDIRECT,
14 IS_ITERABLE, IS_LONG, IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE,
15 IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, PHP_INI_ALL,
16 PHP_INI_PERDIR, PHP_INI_SYSTEM, PHP_INI_USER, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS,
17 ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED,
18 ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING,
19 ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK,
20 ZEND_ACC_HAS_RETURN_TYPE, ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
21 ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
22 ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
23 ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
24 ZEND_ACC_RESOLVED_PARENT, ZEND_ACC_RETURN_REFERENCE, ZEND_ACC_STATIC, ZEND_ACC_STRICT_TYPES,
25 ZEND_ACC_TOP_LEVEL, ZEND_ACC_TRAIT, ZEND_ACC_TRAIT_CLONE, ZEND_ACC_UNRESOLVED_VARIANCE,
26 ZEND_ACC_USES_THIS, ZEND_ACC_USE_GUARDS, ZEND_ACC_VARIADIC, ZEND_EVAL_CODE,
27 ZEND_HAS_STATIC_IN_METHODS, ZEND_INTERNAL_FUNCTION, ZEND_USER_FUNCTION, Z_TYPE_FLAGS_SHIFT,
28 _IS_BOOL,
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}
93
94bitflags! {
95 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
97 pub struct ClassFlags: u32 {
98 const Final = ZEND_ACC_FINAL;
100 const Abstract = ZEND_ACC_ABSTRACT;
102 const Immutable = ZEND_ACC_IMMUTABLE;
105 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
107 const TopLevel = ZEND_ACC_TOP_LEVEL;
109 const Preloaded = ZEND_ACC_PRELOADED;
111
112 const Interface = ZEND_ACC_INTERFACE;
114 const Trait = ZEND_ACC_TRAIT;
116 const AnonymousClass = ZEND_ACC_ANON_CLASS;
118 #[cfg(php81)]
120 const Enum = ZEND_ACC_ENUM;
121 const Linked = ZEND_ACC_LINKED;
123 const ImplicitAbstractClass = ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
125 const UseGuards = ZEND_ACC_USE_GUARDS;
127
128 const ConstantsUpdated = ZEND_ACC_CONSTANTS_UPDATED;
130 const NoDynamicProperties = ZEND_ACC_NO_DYNAMIC_PROPERTIES;
132 const HasStaticInMethods = ZEND_HAS_STATIC_IN_METHODS;
134 #[cfg(not(php82))]
136 const ReuseGetIterator = ZEND_ACC_REUSE_GET_ITERATOR;
137 const ResolvedParent = ZEND_ACC_RESOLVED_PARENT;
139 const ResolvedInterfaces = ZEND_ACC_RESOLVED_INTERFACES;
141 const UnresolvedVariance = ZEND_ACC_UNRESOLVED_VARIANCE;
143 const NearlyLinked = ZEND_ACC_NEARLY_LINKED;
145
146 #[cfg(php81)]
148 const NotSerializable = crate::ffi::ZEND_ACC_NOT_SERIALIZABLE;
149 }
150}
151
152bitflags! {
153 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
155 pub struct MethodFlags: u32 {
156 const Public = ZEND_ACC_PUBLIC;
158 const Protected = ZEND_ACC_PROTECTED;
160 const Private = ZEND_ACC_PRIVATE;
162 const Changed = ZEND_ACC_CHANGED;
164 const Static = ZEND_ACC_STATIC;
166 const Final = ZEND_ACC_FINAL;
168 const Abstract = ZEND_ACC_ABSTRACT;
170 const Immutable = ZEND_ACC_IMMUTABLE;
173 const HasTypeHints = ZEND_ACC_HAS_TYPE_HINTS;
175 const TopLevel = ZEND_ACC_TOP_LEVEL;
177 const Preloaded = ZEND_ACC_PRELOADED;
179
180 const Deprecated = ZEND_ACC_DEPRECATED;
182 const ReturnReference = ZEND_ACC_RETURN_REFERENCE;
184 const HasReturnType = ZEND_ACC_HAS_RETURN_TYPE;
186 const Variadic = ZEND_ACC_VARIADIC;
188 const HasFinallyBlock = ZEND_ACC_HAS_FINALLY_BLOCK;
190 const EarlyBinding = ZEND_ACC_EARLY_BINDING;
192 const UsesThis = ZEND_ACC_USES_THIS;
194 const CallViaTrampoline = ZEND_ACC_CALL_VIA_TRAMPOLINE;
200 const NeverCache = ZEND_ACC_NEVER_CACHE;
202 const TraitClone = ZEND_ACC_TRAIT_CLONE;
204 const IsConstructor = ZEND_ACC_CTOR;
206 const Closure = ZEND_ACC_CLOSURE;
208 const FakeClosure = ZEND_ACC_FAKE_CLOSURE;
210 const Generator = ZEND_ACC_GENERATOR;
212 const DonePassTwo = ZEND_ACC_DONE_PASS_TWO;
214 const HeapRTCache = ZEND_ACC_HEAP_RT_CACHE;
216 const StrictTypes = ZEND_ACC_STRICT_TYPES;
218 }
219}
220
221bitflags! {
222 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
224 pub struct PropertyFlags: u32 {
225 const Public = ZEND_ACC_PUBLIC;
227 const Protected = ZEND_ACC_PROTECTED;
229 const Private = ZEND_ACC_PRIVATE;
231 const Changed = ZEND_ACC_CHANGED;
233 const Static = ZEND_ACC_STATIC;
235 const Promoted = ZEND_ACC_PROMOTED;
237 }
238}
239
240bitflags! {
241 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
243 pub struct ConstantFlags: u32 {
244 const Public = ZEND_ACC_PUBLIC;
246 const Protected = ZEND_ACC_PROTECTED;
248 const Private = ZEND_ACC_PRIVATE;
250 const Promoted = ZEND_ACC_PROMOTED;
252 }
253}
254
255bitflags! {
256 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
258 pub struct GlobalConstantFlags: u32 {
259 #[deprecated(note = "No longer used -- always case-sensitive")]
261 const CaseSensitive = CONST_CS;
262 const Persistent = CONST_PERSISTENT;
264 const NoFileCache = CONST_NO_FILE_CACHE;
266 const Deprecated = CONST_DEPRECATED;
268 }
269}
270
271bitflags! {
272 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
274 pub struct ZendResult: i32 {
275 const Success = 0;
277 const Failure = -1;
279 }
280}
281
282bitflags! {
283 pub struct IniEntryPermission: u32 {
285 const User = PHP_INI_USER;
287 const PerDir = PHP_INI_PERDIR;
289 const System = PHP_INI_SYSTEM;
291 const All = PHP_INI_ALL;
293 }
294}
295
296bitflags! {
297 pub struct ErrorType: u32 {
299 const Error = E_ERROR;
301 const Warning = E_WARNING;
303 const Parse = E_PARSE;
305 const Notice = E_NOTICE;
307 const CoreError = E_CORE_ERROR;
309 const CoreWarning = E_CORE_WARNING;
311 const CompileError = E_COMPILE_ERROR;
313 const CompileWarning = E_COMPILE_WARNING;
315 #[cfg_attr(php84, deprecated = "`E_USER_ERROR` is deprecated since PHP 8.4. Throw an exception instead.")]
317 const UserError = E_USER_ERROR;
318 const UserWarning = E_USER_WARNING;
320 const UserNotice = E_USER_NOTICE;
322 const Strict = E_STRICT;
324 const RecoverableError = E_RECOVERABLE_ERROR;
326 const Deprecated = E_DEPRECATED;
328 const UserDeprecated = E_USER_DEPRECATED;
330 }
331}
332
333#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
335pub enum FunctionType {
336 Internal,
338 User,
340 Eval,
342}
343
344impl From<u8> for FunctionType {
345 #[allow(clippy::bad_bit_mask)]
346 fn from(value: u8) -> Self {
347 match value.into() {
348 ZEND_INTERNAL_FUNCTION => Self::Internal,
349 ZEND_USER_FUNCTION => Self::User,
350 ZEND_EVAL_CODE => Self::Eval,
351 _ => panic!("Unknown function type: {value}"),
352 }
353 }
354}
355
356#[repr(C, u8)]
358#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
359pub enum DataType {
360 Undef,
362 Null,
364 False,
366 True,
368 Long,
370 Double,
372 String,
374 Array,
376 Iterable,
378 Object(Option<&'static str>),
380 Resource,
382 Reference,
384 Callable,
386 ConstantExpression,
388 Void,
390 Mixed,
392 Bool,
394 Ptr,
396 Indirect,
398}
399
400impl Default for DataType {
401 fn default() -> Self {
402 Self::Void
403 }
404}
405
406impl DataType {
407 #[must_use]
409 pub const fn as_u32(&self) -> u32 {
410 match self {
411 DataType::Undef => IS_UNDEF,
412 DataType::Null => IS_NULL,
413 DataType::False => IS_FALSE,
414 DataType::True => IS_TRUE,
415 DataType::Long => IS_LONG,
416 DataType::Double => IS_DOUBLE,
417 DataType::String => IS_STRING,
418 DataType::Array => IS_ARRAY,
419 DataType::Object(_) => IS_OBJECT,
420 DataType::Resource | DataType::Reference => IS_RESOURCE,
421 DataType::Indirect => IS_INDIRECT,
422 DataType::Callable => IS_CALLABLE,
423 DataType::ConstantExpression => IS_CONSTANT_AST,
424 DataType::Void => IS_VOID,
425 DataType::Mixed => IS_MIXED,
426 DataType::Bool => _IS_BOOL,
427 DataType::Ptr => IS_PTR,
428 DataType::Iterable => IS_ITERABLE,
429 }
430 }
431}
432
433impl TryFrom<ZvalTypeFlags> for DataType {
445 type Error = Error;
446
447 fn try_from(value: ZvalTypeFlags) -> Result<Self> {
448 macro_rules! contains {
449 ($t: ident) => {
450 if value.contains(ZvalTypeFlags::$t) {
451 return Ok(DataType::$t);
452 }
453 };
454 }
455
456 contains!(Undef);
457 contains!(Null);
458 contains!(False);
459 contains!(True);
460 contains!(False);
461 contains!(Long);
462 contains!(Double);
463 contains!(String);
464 contains!(Array);
465 contains!(Resource);
466 contains!(Callable);
467 contains!(ConstantExpression);
468 contains!(Void);
469
470 if value.contains(ZvalTypeFlags::Object) {
471 return Ok(DataType::Object(None));
472 }
473
474 Err(Error::UnknownDatatype(0))
475 }
476}
477
478impl From<u32> for DataType {
479 #[allow(clippy::bad_bit_mask)]
480 fn from(value: u32) -> Self {
481 macro_rules! contains {
482 ($c: ident, $t: ident) => {
483 if (value & $c) == $c {
484 return DataType::$t;
485 }
486 };
487 }
488
489 contains!(IS_VOID, Void);
490 contains!(IS_PTR, Ptr);
491 contains!(IS_INDIRECT, Indirect);
492 contains!(IS_CALLABLE, Callable);
493 contains!(IS_CONSTANT_AST, ConstantExpression);
494 contains!(IS_REFERENCE, Reference);
495 contains!(IS_RESOURCE, Resource);
496 contains!(IS_ARRAY, Array);
497 contains!(IS_STRING, String);
498 contains!(IS_DOUBLE, Double);
499 contains!(IS_LONG, Long);
500 contains!(IS_TRUE, True);
501 contains!(IS_FALSE, False);
502 contains!(IS_NULL, Null);
503
504 if (value & IS_OBJECT) == IS_OBJECT {
505 return DataType::Object(None);
506 }
507
508 contains!(IS_UNDEF, Undef);
509
510 DataType::Mixed
511 }
512}
513
514impl Display for DataType {
515 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
516 match self {
517 DataType::Undef => write!(f, "Undefined"),
518 DataType::Null => write!(f, "Null"),
519 DataType::False => write!(f, "False"),
520 DataType::True => write!(f, "True"),
521 DataType::Long => write!(f, "Long"),
522 DataType::Double => write!(f, "Double"),
523 DataType::String => write!(f, "String"),
524 DataType::Array => write!(f, "Array"),
525 DataType::Object(obj) => write!(f, "{}", obj.as_deref().unwrap_or("Object")),
526 DataType::Resource => write!(f, "Resource"),
527 DataType::Reference => write!(f, "Reference"),
528 DataType::Callable => write!(f, "Callable"),
529 DataType::ConstantExpression => write!(f, "Constant Expression"),
530 DataType::Void => write!(f, "Void"),
531 DataType::Bool => write!(f, "Bool"),
532 DataType::Mixed => write!(f, "Mixed"),
533 DataType::Ptr => write!(f, "Pointer"),
534 DataType::Indirect => write!(f, "Indirect"),
535 DataType::Iterable => write!(f, "Iterable"),
536 }
537 }
538}
539
540#[cfg(test)]
541mod tests {
542 #![allow(clippy::unnecessary_fallible_conversions)]
543 use super::DataType;
544 use crate::ffi::{
545 IS_ARRAY, IS_ARRAY_EX, IS_CONSTANT_AST, IS_CONSTANT_AST_EX, IS_DOUBLE, IS_FALSE,
546 IS_INDIRECT, IS_INTERNED_STRING_EX, IS_LONG, IS_NULL, IS_OBJECT, IS_OBJECT_EX, IS_PTR,
547 IS_REFERENCE, IS_REFERENCE_EX, IS_RESOURCE, IS_RESOURCE_EX, IS_STRING, IS_STRING_EX,
548 IS_TRUE, IS_UNDEF, IS_VOID,
549 };
550 use std::convert::TryFrom;
551
552 #[test]
553 fn test_datatype() {
554 macro_rules! test {
555 ($c: ident, $t: ident) => {
556 assert_eq!(DataType::try_from($c), Ok(DataType::$t));
557 };
558 }
559
560 test!(IS_UNDEF, Undef);
561 test!(IS_NULL, Null);
562 test!(IS_FALSE, False);
563 test!(IS_TRUE, True);
564 test!(IS_LONG, Long);
565 test!(IS_DOUBLE, Double);
566 test!(IS_STRING, String);
567 test!(IS_ARRAY, Array);
568 assert_eq!(DataType::try_from(IS_OBJECT), Ok(DataType::Object(None)));
569 test!(IS_RESOURCE, Resource);
570 test!(IS_REFERENCE, Reference);
571 test!(IS_CONSTANT_AST, ConstantExpression);
572 test!(IS_INDIRECT, Indirect);
573 test!(IS_VOID, Void);
574 test!(IS_PTR, Ptr);
575
576 test!(IS_INTERNED_STRING_EX, String);
577 test!(IS_STRING_EX, String);
578 test!(IS_ARRAY_EX, Array);
579 assert_eq!(DataType::try_from(IS_OBJECT_EX), Ok(DataType::Object(None)));
580 test!(IS_RESOURCE_EX, Resource);
581 test!(IS_REFERENCE_EX, Reference);
582 test!(IS_CONSTANT_AST_EX, ConstantExpression);
583 }
584}