Skip to main content

luaur_analysis/methods/
replace_generics_replace_generics.rs

1//! Node: `cxx:Method:Luau.Analysis:Analysis/include/Luau/Instantiation.h:21:ReplaceGenerics`
2//! Source: `Analysis/include/Luau/Instantiation.h` (Instantiation.h:21-37, hand-ported)
3use crate::records::builtin_types::BuiltinTypes;
4use crate::records::replace_generics::ReplaceGenerics;
5use crate::records::scope::Scope;
6use crate::records::substitution::Substitution;
7use crate::records::tarjan::SubstitutionVtable;
8use crate::records::txn_log::TxnLog;
9use crate::records::type_arena::TypeArena;
10use crate::records::type_level::TypeLevel;
11use crate::type_aliases::type_id::TypeId;
12use crate::type_aliases::type_pack_id::TypePackId;
13use alloc::vec::Vec;
14use core::ffi::c_void;
15
16fn replace_generics_is_dirty_ty(owner: *mut c_void, ty: TypeId) -> bool {
17    unsafe { (*(owner as *mut ReplaceGenerics)).is_dirty_type_id(ty) }
18}
19
20fn replace_generics_is_dirty_tp(owner: *mut c_void, tp: TypePackId) -> bool {
21    unsafe { (*(owner as *mut ReplaceGenerics)).is_dirty_type_pack_id(tp) }
22}
23
24fn replace_generics_clean_ty(owner: *mut c_void, ty: TypeId) -> TypeId {
25    unsafe { (*(owner as *mut ReplaceGenerics)).clean_type_id(ty) }
26}
27
28fn replace_generics_clean_tp(owner: *mut c_void, tp: TypePackId) -> TypePackId {
29    unsafe { (*(owner as *mut ReplaceGenerics)).clean_type_pack_id(tp) }
30}
31
32fn replace_generics_found_dirty_ty(owner: *mut c_void, ty: TypeId) {
33    unsafe {
34        (*(owner as *mut ReplaceGenerics))
35            .base
36            .found_dirty_type_id(ty)
37    }
38}
39
40fn replace_generics_found_dirty_tp(owner: *mut c_void, tp: TypePackId) {
41    unsafe {
42        (*(owner as *mut ReplaceGenerics))
43            .base
44            .found_dirty_type_pack_id(tp)
45    }
46}
47
48fn replace_generics_ignore_children_ty(owner: *mut c_void, ty: TypeId) -> bool {
49    unsafe { (*(owner as *mut ReplaceGenerics)).ignore_children(ty) }
50}
51
52fn replace_generics_ignore_children_tp(_owner: *mut c_void, _tp: TypePackId) -> bool {
53    false
54}
55
56impl ReplaceGenerics {
57    /// C++ `ReplaceGenerics(const TxnLog* log, TypeArena* arena, NotNull<BuiltinTypes> builtinTypes,
58    /// TypeLevel level, Scope* scope, const std::vector<TypeId>& generics,
59    /// const std::vector<TypePackId>& genericPacks) : Substitution(log, arena), ...`.
60    pub fn replace_generics_new(
61        log: *const TxnLog,
62        arena: *mut TypeArena,
63        builtin_types: *mut BuiltinTypes,
64        level: TypeLevel,
65        scope: *mut Scope,
66        generics: Vec<TypeId>,
67        generic_packs: Vec<TypePackId>,
68    ) -> Self {
69        ReplaceGenerics {
70            base: Substitution::substitution_new(log, arena),
71            builtin_types,
72            level,
73            scope,
74            generics,
75            generic_packs,
76        }
77    }
78
79    fn install_substitution_vtable(&mut self) {
80        let owner = self as *mut ReplaceGenerics as *mut c_void;
81        self.base.base.vtable = SubstitutionVtable {
82            owner,
83            is_dirty_ty: Some(replace_generics_is_dirty_ty),
84            is_dirty_tp: Some(replace_generics_is_dirty_tp),
85            clean_ty: Some(replace_generics_clean_ty),
86            clean_tp: Some(replace_generics_clean_tp),
87            found_dirty_ty: Some(replace_generics_found_dirty_ty),
88            found_dirty_tp: Some(replace_generics_found_dirty_tp),
89            ignore_children_ty: Some(replace_generics_ignore_children_ty),
90            ignore_children_tp: Some(replace_generics_ignore_children_tp),
91            ignore_children_visit_ty: Some(replace_generics_ignore_children_ty),
92            ignore_children_visit_tp: Some(replace_generics_ignore_children_tp),
93        };
94    }
95
96    pub fn substitute_type_id(&mut self, ty: TypeId) -> Option<TypeId> {
97        self.install_substitution_vtable();
98        self.base.substitute_type_id(ty)
99    }
100
101    pub fn substitute_type_pack_id(&mut self, tp: TypePackId) -> Option<TypePackId> {
102        self.install_substitution_vtable();
103        self.base.substitute_type_pack_id(tp)
104    }
105}