1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::{ffi::c_void, ptr};
use crate::{
error::{Error, Result},
ffi::{
_IS_BOOL, _ZEND_IS_VARIADIC_BIT, _ZEND_SEND_MODE_SHIFT, _ZEND_TYPE_NULLABLE_BIT, IS_ARRAY,
IS_DOUBLE, IS_ITERABLE, IS_LONG, IS_MIXED, IS_OBJECT, IS_STRING, MAY_BE_ANY, MAY_BE_BOOL,
zend_type,
},
flags::{DataType, DataTypeExt},
};
/// Type mask PHP uses for the `iterable` pseudo-type in arginfo.
///
/// PHP 8.1 encodes it as `1 << IS_ITERABLE`, which is what `MAY_BE_ITERABLE`
/// expands to. PHP 8.2 replaced that with a dedicated
/// `_ZEND_TYPE_ITERABLE_BIT`, so `1 << IS_ITERABLE` no longer describes an
/// iterable and the engine rejects every argument passed to such a parameter.
#[cfg(php82)]
const ITERABLE_MASK: u32 = crate::ffi::_ZEND_TYPE_ITERABLE_BIT;
#[cfg(not(php82))]
const ITERABLE_MASK: u32 = 1 << IS_ITERABLE;
/// Internal Zend type.
pub type ZendType = zend_type;
impl ZendType {
/// Builds an empty Zend type container.
///
/// # Parameters
///
/// * `pass_by_ref` - Whether the value should be passed by reference.
/// * `is_variadic` - Whether this type represents a variadic argument.
#[must_use]
pub fn empty(pass_by_ref: bool, is_variadic: bool) -> Self {
Self {
ptr: ptr::null_mut::<c_void>(),
type_mask: Self::arg_info_flags(pass_by_ref, is_variadic),
}
}
/// Attempts to create a zend type for a given datatype. Returns an option
/// containing the type.
///
/// Returns [`None`] if the data type was a class object where the class
/// name could not be converted into a C string (i.e. contained
/// NUL-bytes).
///
/// # Parameters
///
/// * `type_` - Data type to create zend type for.
/// * `pass_by_ref` - Whether the type should be passed by reference.
/// * `is_variadic` - Whether the type is for a variadic argument.
/// * `allow_null` - Whether the type should allow null to be passed in
/// place.
#[must_use]
pub fn empty_from_type(
type_: DataType,
pass_by_ref: bool,
is_variadic: bool,
allow_null: bool,
) -> Option<Self> {
match type_.class_name() {
Some(class) => Self::empty_from_class_type(class, pass_by_ref, is_variadic, allow_null),
None => Some(Self::empty_from_primitive_type(
type_,
pass_by_ref,
is_variadic,
allow_null,
)),
}
}
/// Attempts to create a zend type for a class object type. Returns an
/// option containing the type if successful.
///
/// Returns [`None`] if the data type was a class object where the class
/// name could not be converted into a C string (i.e. contained
/// NUL-bytes).
///
/// # Parameters
///
/// * `class_name` - Name of the class parameter.
/// * `pass_by_ref` - Whether the type should be passed by reference.
/// * `is_variadic` - Whether the type is for a variadic argument.
/// * `allow_null` - Whether the type should allow null to be passed in
/// place.
fn empty_from_class_type(
class_name: &str,
pass_by_ref: bool,
is_variadic: bool,
allow_null: bool,
) -> Option<Self> {
let mut flags = Self::arg_info_flags(pass_by_ref, is_variadic);
if allow_null {
flags |= _ZEND_TYPE_NULLABLE_BIT;
}
cfg_if::cfg_if! {
if #[cfg(php83)] {
flags |= crate::ffi::_ZEND_TYPE_LITERAL_NAME_BIT
} else {
flags |= crate::ffi::_ZEND_TYPE_NAME_BIT
}
}
Some(Self {
ptr: std::ffi::CString::new(class_name)
.ok()?
.into_raw()
.cast::<c_void>(),
type_mask: flags,
})
}
/// Attempts to create a zend type for a primitive PHP type.
///
/// # Parameters
///
/// * `type_` - Data type to create zend type for.
/// * `pass_by_ref` - Whether the type should be passed by reference.
/// * `is_variadic` - Whether the type is for a variadic argument.
/// * `allow_null` - Whether the type should allow null to be passed in
/// place.
///
/// # Panics
///
/// Panics if the given `type_` is for a class object type.
fn empty_from_primitive_type(
type_: DataType,
pass_by_ref: bool,
is_variadic: bool,
allow_null: bool,
) -> Self {
assert!(type_.class_name().is_none());
Self {
ptr: ptr::null_mut::<c_void>(),
type_mask: Self::type_init_code(type_, pass_by_ref, is_variadic, allow_null),
}
}
/// The type mask of `ty` when it is one of the types every declaration can
/// hold as such: `bool`, `int`, `float`, `string`, `array` and `object`
/// (a `DataType::Object` without a class name). `None` for every other
/// type, which a caller allows (`mixed`, a class, ...) or refuses on its
/// own terms.
#[must_use]
pub(crate) fn scalar_type_mask(ty: DataType) -> Option<u32> {
Some(match ty {
DataType::Bool => MAY_BE_BOOL,
DataType::Long => 1 << IS_LONG,
DataType::Double => 1 << IS_DOUBLE,
DataType::String => 1 << IS_STRING,
DataType::Array => 1 << IS_ARRAY,
DataType::Object(_) if ty.class_name().is_none() => 1 << IS_OBJECT,
DataType::Object(_)
| DataType::Undef
| DataType::Null
| DataType::False
| DataType::True
| DataType::Iterable
| DataType::Resource
| DataType::Reference
| DataType::Callable
| DataType::ConstantExpression
| DataType::Indirect
| DataType::Void
| DataType::Mixed
| DataType::Ptr => return None,
})
}
/// Adds the `union` members to `type_`, built from `first`, so it declares
/// `first|union...`. `first` and each member are one of the types of
/// [`ZendType::scalar_type_mask`], or `null` for a member, which makes the
/// type nullable; a class type would need a type list, and every other type
/// (`iterable`, `resource`, `void`, `mixed`, ...) cannot be a union member.
///
/// # Errors
///
/// [`Error::InvalidUnionType`] naming `declaration` (the parameter or
/// function) when `first` or a member is not such a type.
pub(crate) fn add_union(
type_: &mut Self,
declaration: &str,
first: DataType,
union: &[DataType],
) -> Result<()> {
if union.is_empty() {
return Ok(());
}
let refused =
|ty: DataType| Error::InvalidUnionType(format!("{declaration}: {ty} in a union"));
if Self::scalar_type_mask(first).is_none() {
return Err(refused(first));
}
for &member in union {
type_.type_mask |= match member {
DataType::Null => _ZEND_TYPE_NULLABLE_BIT,
_ => Self::scalar_type_mask(member).ok_or_else(|| refused(member))?,
};
}
Ok(())
}
/// Calculates the internal flags of the type.
/// Translation of of the `_ZEND_ARG_INFO_FLAGS` macro from
/// `zend_API.h:110`.
///
/// # Parameters
///
/// * `pass_by_ref` - Whether the value should be passed by reference.
/// * `is_variadic` - Whether this type represents a variadic argument.
pub(crate) fn arg_info_flags(pass_by_ref: bool, is_variadic: bool) -> u32 {
(u32::from(pass_by_ref) << _ZEND_SEND_MODE_SHIFT)
| (if is_variadic {
_ZEND_IS_VARIADIC_BIT
} else {
0
})
}
/// Calculates the internal flags of the type.
/// Translation of the `ZEND_TYPE_INIT_CODE` macro from `zend_API.h:163`.
///
/// # Parameters
///
/// * `type_` - The type to initialize the Zend type with.
/// * `pass_by_ref` - Whether the value should be passed by reference.
/// * `is_variadic` - Whether this type represents a variadic argument.
/// * `allow_null` - Whether the value can be null.
pub(crate) fn type_init_code(
type_: DataType,
pass_by_ref: bool,
is_variadic: bool,
allow_null: bool,
) -> u32 {
let type_ = type_.as_u32();
(if type_ == _IS_BOOL {
MAY_BE_BOOL
} else if type_ == IS_ITERABLE {
ITERABLE_MASK
} else if type_ == IS_MIXED {
MAY_BE_ANY
} else {
1 << type_
}) | (if allow_null {
_ZEND_TYPE_NULLABLE_BIT
} else {
0
}) | Self::arg_info_flags(pass_by_ref, is_variadic)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scalar_type_mask_covers_every_data_type() {
let cases = [
(DataType::Undef, None),
(DataType::Null, None),
(DataType::False, None),
(DataType::True, None),
(DataType::Long, Some(1 << IS_LONG)),
(DataType::Double, Some(1 << IS_DOUBLE)),
(DataType::String, Some(1 << IS_STRING)),
(DataType::Array, Some(1 << IS_ARRAY)),
(DataType::Iterable, None),
(DataType::Object("".into()), Some(1 << IS_OBJECT)),
(DataType::Object("Foo".into()), None),
(DataType::Resource, None),
(DataType::Reference, None),
(DataType::Callable, None),
(DataType::ConstantExpression, None),
(DataType::Indirect, None),
(DataType::Void, None),
(DataType::Mixed, None),
(DataType::Bool, Some(MAY_BE_BOOL)),
(DataType::Ptr, None),
];
for (ty, mask) in cases {
assert_eq!(ZendType::scalar_type_mask(ty), mask, "{ty:?}");
}
}
}