diff --git a/crates/stub-parser/src/builder.rs b/crates/stub-parser/src/builder.rs index 539aca8..6a67dfb 100644 --- a/crates/stub-parser/src/builder.rs +++ b/crates/stub-parser/src/builder.rs @@ -2,11 +2,12 @@ use crate::VerificationMode; use crate::imports::ImportsUsed; use crate::types::ArgInfoTypeRepr; use crate::verification::{VERIFICATION_RSPHP_SYS_IMPORTS, VerificationData}; -use php_ast::ast::{BuiltinType, NameKind, Visibility}; +use php_ast::ast::{BuiltinType, NameKind, UnaryPrefixOp, Visibility}; use php_ast::owned::{ Attribute, ClassConstDecl, ClassDecl, ClassMemberKind, Comment, EnumCase, EnumDecl, EnumMemberKind, Expr, ExprKind, FunctionDecl, Ident, InterfaceDecl, MethodDecl, Name, NamespaceBody, NamespaceDecl, Param, PropertyDecl, Stmt, StmtKind, TypeHint, TypeHintKind, + UnaryPrefixExpr, }; use php_ast::span::Span; use regex::Regex; @@ -1199,6 +1200,19 @@ impl ArgInfoBuilder<'_> { self.scope.add_import("rsphp_macros_c", "ZVAL_LONG"); format!("ZVAL_LONG!(&mut value, {num});") } + ExprKind::UnaryPrefix(UnaryPrefixExpr { + op: UnaryPrefixOp::Negate, + operand: negated, + }) => match &**negated { + Expr { + kind: ExprKind::Int(num), + span: _, + } => { + self.scope.add_import("rsphp_macros_c", "ZVAL_LONG"); + format!("ZVAL_LONG!(&mut value, -{num});") + } + _ => return Err(format!("Unhandled constant initialization: {constant:#?}")), + }, ExprKind::Null => { self.scope.add_import("rsphp_macros_c", "ZVAL_NULL"); "ZVAL_NULL!(&mut value);".to_owned() diff --git a/crates/stub-parser/tests/cases/class_constants.expected b/crates/stub-parser/tests/cases/class_constants.expected index 65f0612..37671a4 100644 --- a/crates/stub-parser/tests/cases/class_constants.expected +++ b/crates/stub-parser/tests/cases/class_constants.expected @@ -191,3 +191,37 @@ pub(crate) mod ImplicitPublicConstant { class_entry } } +/// Module for `IntlDateFormatter` class. +pub(crate) mod IntlDateFormatter { + use rsphp_macros_c::{ZEND_TYPE_INIT_MASK, ZVAL_LONG}; + use rsphp_sys::{MAY_BE_LONG, ZEND_ACC_PUBLIC, zend_class_entry, zend_declare_typed_class_constant, zend_string, zend_string_init_interned, zend_string_release_ex, zval}; + + /// Register the `IntlDateFormatter` class. + pub(crate) fn register_class() -> *mut zend_class_entry { + let mut ce: zend_class_entry = unsafe { std::mem::zeroed() }; + rsphp_macros_c::INIT_CLASS_ENTRY!(ce, "IntlDateFormatter", NULL); + let class_entry: *mut zend_class_entry = unsafe { + rsphp_sys::zend_register_internal_class_with_flags( + std::ptr::from_mut::(&mut ce), + std::ptr::null_mut(), + 0, + ) + }; + + unsafe { + let mut value: zval = std::mem::zeroed(); + ZVAL_LONG!(&mut value, -16); + let name: *mut zend_string = (zend_string_init_interned.expect("zend_string_init_interned should be set"))( + c"PROLEPTIC_GREGORIAN".as_ptr(), + "PROLEPTIC_GREGORIAN".len(), + true, + ); + zend_declare_typed_class_constant( + class_entry, name, std::ptr::from_mut::(&mut value), ZEND_ACC_PUBLIC as i32, + std::ptr::null_mut(), ZEND_TYPE_INIT_MASK!(MAY_BE_LONG) + ); + zend_string_release_ex(name, true); + } + class_entry + } +} diff --git a/crates/stub-parser/tests/cases/class_constants.stub.php b/crates/stub-parser/tests/cases/class_constants.stub.php index c22677e..12679af 100644 --- a/crates/stub-parser/tests/cases/class_constants.stub.php +++ b/crates/stub-parser/tests/cases/class_constants.stub.php @@ -24,3 +24,9 @@ class ConstantValues { class ImplicitPublicConstant { const int FOO = 5; } + +class IntlDateFormatter { + + public const int PROLEPTIC_GREGORIAN = -16; + +}