Skip to main content

mago_codex/ttype/
shared.rs

1//! A collection of shared, static, and lazily-initialized `TAtomic` types.
2//!
3//! This module provides canonical, reusable instances for common PHP types.
4//! Using these constants avoids repeated allocations for frequently used types.
5
6use std::sync::Arc;
7use std::sync::LazyLock;
8
9use mago_atom::empty_atom;
10
11use crate::ttype::atomic::TAtomic;
12use crate::ttype::atomic::array::TArray;
13use crate::ttype::atomic::array::keyed::TKeyedArray;
14use crate::ttype::atomic::callable::TCallable;
15use crate::ttype::atomic::callable::TCallableSignature;
16use crate::ttype::atomic::iterable::TIterable;
17use crate::ttype::atomic::mixed::TMixed;
18use crate::ttype::atomic::object::TObject;
19use crate::ttype::atomic::resource::TResource;
20use crate::ttype::atomic::scalar::TScalar;
21use crate::ttype::atomic::scalar::int::TInteger;
22use crate::ttype::atomic::scalar::string::TString;
23use crate::ttype::atomic::scalar::string::TStringLiteral;
24use crate::ttype::get_arraykey;
25use crate::ttype::get_mixed;
26
27use super::atomic::scalar::string::TStringCasing;
28
29/// A static `TAtomic` representing the integer literal `1`.
30pub const ONE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::literal_int(1));
31/// A static `TAtomic` representing the integer literal `0`.
32pub const ZERO_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::literal_int(0));
33/// A static `TAtomic` representing the integer literal `-1`.
34pub const MINUS_ONE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::literal_int(-1));
35/// A static `TAtomic` representing the general `int` type.
36pub const INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::int());
37/// A static `TAtomic` representing a positive integer (`positive-int` or `int<1, max>`).
38pub const POSITIVE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Integer(TInteger::positive()));
39/// A static `TAtomic` representing a non-positive integer (`non-positive-int` or `int<min, 0>`).
40pub const NON_POSITIVE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Integer(TInteger::non_positive()));
41/// A static `TAtomic` representing a negative integer (`negative-int` or `int<min, -1>`).
42pub const NEGATIVE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Integer(TInteger::negative()));
43/// A static `TAtomic` representing a non-negative integer (`non-negative-int` or `int<0, max>`).
44pub const NON_NEGATIVE_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Integer(TInteger::non_negative()));
45/// A static `TAtomic` representing a `literal-int` where the value is unknown.
46pub const UNSPECIFIED_LITERAL_INT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Integer(TInteger::UnspecifiedLiteral));
47/// A static `TAtomic` for the general `string` type.
48pub const STRING_ATOMIC: &TAtomic =
49    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, false, false, TStringCasing::Unspecified)));
50/// A static `TAtomic` for a `lowercase-string`.
51pub const LOWERCASE_STRING_ATOMIC: &TAtomic =
52    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, false, false, TStringCasing::Lowercase)));
53/// A static `TAtomic` for a `uppercase-string`.
54pub const UPPERCASE_STRING_ATOMIC: &TAtomic =
55    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, false, false, TStringCasing::Uppercase)));
56/// A static `TAtomic` for a `non-empty-string`.
57pub const NON_EMPTY_STRING_ATOMIC: &TAtomic =
58    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, true, false, TStringCasing::Unspecified)));
59/// A static `TAtomic` for a `non-empty-lowercase-string`.
60pub const NON_EMPTY_LOWERCASE_STRING_ATOMIC: &TAtomic =
61    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, true, false, TStringCasing::Lowercase)));
62/// A static `TAtomic` for a `non-empty-uppercase-string`.
63pub const NON_EMPTY_UPPERCASE_STRING_ATOMIC: &TAtomic =
64    &TAtomic::Scalar(TScalar::String(TString::new(None, false, false, true, false, TStringCasing::Uppercase)));
65/// A static `TAtomic` for a `truthy-string`.
66pub const TRUTHY_STRING_ATOMIC: &TAtomic =
67    &TAtomic::Scalar(TScalar::String(TString::new(None, false, true, false, false, TStringCasing::Unspecified)));
68/// A static `TAtomic` for a `truthy-lowercase-string`.
69pub const TRUTHY_LOWERCASE_STRING_ATOMIC: &TAtomic =
70    &TAtomic::Scalar(TScalar::String(TString::new(None, false, true, false, false, TStringCasing::Lowercase)));
71/// A static `TAtomic` for a `truthy-uppercase-string`.
72pub const TRUTHY_UPPERCASE_STRING_ATOMIC: &TAtomic =
73    &TAtomic::Scalar(TScalar::String(TString::new(None, false, true, false, false, TStringCasing::Uppercase)));
74/// A static `TAtomic` for a `numeric-string`.
75pub const CALLABLE_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::String(TString::callable()));
76pub const LOWERCASE_CALLABLE_STRING_ATOMIC: &TAtomic =
77    &TAtomic::Scalar(TScalar::String(TString::callable_with_casing(TStringCasing::Lowercase)));
78pub const UPPERCASE_CALLABLE_STRING_ATOMIC: &TAtomic =
79    &TAtomic::Scalar(TScalar::String(TString::callable_with_casing(TStringCasing::Uppercase)));
80pub const NUMERIC_STRING_ATOMIC: &TAtomic =
81    &TAtomic::Scalar(TScalar::String(TString::new(None, true, false, false, false, TStringCasing::Unspecified)));
82/// A static `TAtomic` for a `numeric-string` that is also `truthy`.
83pub const NUMERIC_TRUTHY_STRING_ATOMIC: &TAtomic =
84    &TAtomic::Scalar(TScalar::String(TString::new(None, true, true, false, false, TStringCasing::Unspecified)));
85/// A static `TAtomic` representing the `class-string` type.
86pub const CLASS_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::class_string());
87/// A static `TAtomic` representing the `interface-string` type.
88pub const INTERFACE_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::interface_string());
89/// A static `TAtomic` representing the `enum-string` type.
90pub const ENUM_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::enum_string());
91/// A static `TAtomic` representing the `trait-string` type.
92pub const TRAIT_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::trait_string());
93/// A static `TAtomic` representing the `float` type.
94pub const FLOAT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::float());
95/// A static `TAtomic` representing the `literal-float` type (unspecified literal).
96pub const UNSPECIFIED_LITERAL_FLOAT_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::unspecified_literal_float());
97/// A static `TAtomic` representing the `mixed` type.
98pub const MIXED_ATOMIC: &TAtomic = &TAtomic::Mixed(TMixed::new());
99/// A static `TAtomic` representing a `mixed` type that originates from an `isset()` check inside a loop.
100pub const ISSET_FROM_LOOP_MIXED_ATOMIC: &TAtomic = &TAtomic::Mixed(TMixed::isset_from_loop());
101/// A static `TAtomic` representing the `never` type, which indicates an impossible state.
102pub const NEVER_ATOMIC: &TAtomic = &TAtomic::Never;
103/// A static `TAtomic` representing any value that is not `null`.
104pub const NON_NULL_ATOMIC: &TAtomic = &TAtomic::Mixed(TMixed::non_null());
105/// A static `TAtomic` representing any "falsy" value (e.g., `false`, `0`, `""`, `[]`).
106pub const FALSY_MIXED_ATOMIC: &TAtomic = &TAtomic::Mixed(TMixed::falsy());
107/// A static `TAtomic` representing any "truthy" value.
108pub const TRUTHY_MIXED_ATOMIC: &TAtomic = &TAtomic::Mixed(TMixed::truthy());
109/// A static `TAtomic` representing the `resource` type.
110pub const RESOURCE_ATOMIC: &TAtomic = &TAtomic::Resource(TResource::new(None));
111/// A static `TAtomic` representing an open `resource`.
112pub const OPEN_RESOURCE_ATOMIC: &TAtomic = &TAtomic::Resource(TResource::open());
113/// A static `TAtomic` representing a closed `resource`.
114pub const CLOSED_RESOURCE_ATOMIC: &TAtomic = &TAtomic::Resource(TResource::closed());
115/// A static `TAtomic` used as a temporary placeholder during type reconciliation.
116pub const PLACEHOLDER_ATOMIC: &TAtomic = &TAtomic::Placeholder;
117/// A static `TAtomic` representing the `void` type.
118pub const VOID_ATOMIC: &TAtomic = &TAtomic::Void;
119/// A static `TAtomic` representing the `null` type.
120pub const NULL_ATOMIC: &TAtomic = &TAtomic::Null;
121/// A static `TAtomic` representing the `array-key` type (`int|string`).
122pub const ARRAYKEY_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::ArrayKey);
123/// A static `TAtomic` representing the `bool` type.
124pub const BOOL_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::bool());
125/// A static `TAtomic` representing the literal `false` type.
126pub const FALSE_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::r#false());
127/// A static `TAtomic` representing the literal `true` type.
128pub const TRUE_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::r#true());
129/// A static `TAtomic` representing the general `object` type.
130pub const OBJECT_ATOMIC: &TAtomic = &TAtomic::Object(TObject::Any);
131/// A static `TAtomic` representing the `numeric` type (`int|float|numeric-string`).
132pub const NUMERIC_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Numeric);
133/// A static `TAtomic` representing an empty string literal (`""`).
134pub static EMPTY_STRING_ATOMIC: LazyLock<TAtomic> = LazyLock::new(|| {
135    TAtomic::Scalar(TScalar::String(TString {
136        literal: Some(TStringLiteral::Value(empty_atom())),
137        is_numeric: false,
138        is_truthy: false,
139        is_non_empty: false,
140        is_callable: false,
141        casing: TStringCasing::Unspecified,
142    }))
143});
144/// A static `TAtomic` representing a `literal-string` where the value is unknown.
145pub const UNSPECIFIED_LITERAL_STRING_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::unspecified_literal_string(false));
146/// A static `TAtomic` representing a non-empty `literal-string` where the value is unknown.
147pub const NON_EMPTY_UNSPECIFIED_LITERAL_STRING_ATOMIC: &TAtomic =
148    &TAtomic::Scalar(TScalar::unspecified_literal_string(true));
149/// A static `TAtomic` representing the `scalar` type (`int|float|string|bool`).
150pub const SCALAR_ATOMIC: &TAtomic = &TAtomic::Scalar(TScalar::Generic);
151
152/// A lazily-initialized static `TAtomic` for a mixed iterable (`iterable<mixed, mixed>`).
153pub static MIXED_ITERABLE_ATOMIC: LazyLock<TAtomic> = LazyLock::new(|| {
154    TAtomic::Iterable(TIterable {
155        key_type: Arc::new(get_mixed()),
156        value_type: Arc::new(get_mixed()),
157        intersection_types: None,
158    })
159});
160
161/// A lazily-initialized static `TAtomic` for an empty array (`array<never, never>`).
162pub static EMPTY_KEYED_ARRAY_ATOMIC: LazyLock<TAtomic> =
163    LazyLock::new(|| TAtomic::Array(TArray::Keyed(TKeyedArray::new())));
164/// A lazily-initialized static `TAtomic` for a mixed array (`array<array-key, mixed>`).
165pub static MIXED_KEYED_ARRAY_ATOMIC: LazyLock<TAtomic> = LazyLock::new(|| {
166    TAtomic::Array(TArray::Keyed(TKeyedArray::new_with_parameters(Arc::new(get_arraykey()), Arc::new(get_mixed()))))
167});
168/// A lazily-initialized static `TAtomic` for a mixed callable (`callable`).
169pub static MIXED_CALLABLE_ATOMIC: LazyLock<TAtomic> =
170    LazyLock::new(|| TAtomic::Callable(TCallable::Signature(TCallableSignature::mixed(false))));
171/// A lazily-initialized static `TAtomic` for a mixed closure (`Closure`).
172pub static MIXED_CLOSURE_ATOMIC: LazyLock<TAtomic> =
173    LazyLock::new(|| TAtomic::Callable(TCallable::Signature(TCallableSignature::mixed(true))));
174
175/// A static slice of atomics representing the union type `int|float`.
176pub const INT_FLOAT_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Scalar(TScalar::int()), TAtomic::Scalar(TScalar::float())];
177/// A static slice of atomics representing the union type `int|string`.
178pub const INT_STRING_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Scalar(TScalar::int()), TAtomic::Scalar(TScalar::string())];
179/// A static slice of atomics representing the union type `null|scalar`.
180pub const NULL_SCALAR_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Null, TAtomic::Scalar(TScalar::Generic)];
181/// A static slice of atomics representing the union type `null|string`.
182pub const NULL_STRING_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Null, TAtomic::Scalar(TScalar::string())];
183/// A static slice of atomics representing the union type `null|int`.
184pub const NULL_INT_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Null, TAtomic::Scalar(TScalar::int())];
185/// A static slice of atomics representing the union type `null|float`.
186pub const NULL_FLOAT_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Null, TAtomic::Scalar(TScalar::float())];
187/// A static slice of atomics representing the union type `null|object`.
188pub const NULL_OBJECT_ATOMIC_SLICE: &[TAtomic] = &[TAtomic::Null, TAtomic::Object(TObject::Any)];
189
190/// A static slice of atomics representing the union type `-1|0|1`, commonly
191/// returned by comparison operations like the spaceship operator (`<=>`).
192pub const SIGNUM_RESULT_SLICE: &[TAtomic] = &[
193    TAtomic::Scalar(TScalar::literal_int(-1)),
194    TAtomic::Scalar(TScalar::literal_int(0)),
195    TAtomic::Scalar(TScalar::literal_int(1)),
196];