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