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