Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crates/stub-parser/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions crates/stub-parser/tests/cases/class_constants.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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::<zend_class_entry>(&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::<zval>(&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
}
}
6 changes: 6 additions & 0 deletions crates/stub-parser/tests/cases/class_constants.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ class ConstantValues {
class ImplicitPublicConstant {
const int FOO = 5;
}

class IntlDateFormatter {

public const int PROLEPTIC_GREGORIAN = -16;

}
Loading