File size: 6,569 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use quote::ToTokens;
use syn::{GenericArgument, Ident, Path, PathArguments, Type, TypeParamBound, spanned::Spanned};

pub fn get_register_value_type_ident(struct_ident: &Ident) -> Ident {
    Ident::new(
        &format!("__register_{struct_ident}_value_type"),
        struct_ident.span(),
    )
}

pub fn get_register_trait_methods_ident(trait_ident: &Ident, struct_ident: &Ident) -> Ident {
    Ident::new(
        &format!("__register_{struct_ident}_{trait_ident}_trait_methods"),
        trait_ident.span(),
    )
}

pub fn get_register_trait_impls_ident(trait_ident: &Ident, struct_ident: &Ident) -> Ident {
    Ident::new(
        &format!("__register_{struct_ident}_{trait_ident}_trait_impls"),
        trait_ident.span(),
    )
}

pub fn get_native_function_ident(ident: &Ident) -> Ident {
    Ident::new(
        &format!("{}_FUNCTION", ident.to_string().to_uppercase()),
        ident.span(),
    )
}

pub fn get_trait_type_ident(ident: &Ident) -> Ident {
    Ident::new(
        &format!("{}_TRAIT_TYPE", ident.to_string().to_uppercase()),
        ident.span(),
    )
}

pub fn get_impl_function_ident(struct_ident: &Ident, ident: &Ident) -> Ident {
    Ident::new(
        &format!(
            "{}_IMPL_{}_FUNCTION",
            struct_ident.to_string().to_uppercase(),
            ident.to_string().to_uppercase()
        ),
        ident.span(),
    )
}

pub fn get_inherent_impl_function_ident(ty_ident: &Ident, fn_ident: &Ident) -> Ident {
    Ident::new(
        &format!(
            "{}_IMPL_{}_FUNCTION",
            ty_ident.to_string().to_uppercase(),
            fn_ident.to_string().to_uppercase()
        ),
        fn_ident.span(),
    )
}

pub fn get_inherent_impl_function_id_ident(ty_ident: &Ident, fn_ident: &Ident) -> Ident {
    Ident::new(
        &format!(
            "{}_IMPL_{}_FUNCTION_ID",
            ty_ident.to_string().to_uppercase(),
            fn_ident.to_string().to_uppercase()
        ),
        fn_ident.span(),
    )
}

pub fn get_trait_impl_function_ident(
    struct_ident: &Ident,
    trait_ident: &Ident,
    ident: &Ident,
) -> Ident {
    Ident::new(
        &format!(
            "{}_IMPL_TRAIT_{}_{}_FUNCTION",
            struct_ident.to_string().to_uppercase(),
            trait_ident.to_string().to_uppercase(),
            ident.to_string().to_uppercase()
        ),
        ident.span(),
    )
}

pub fn get_trait_impl_function_id_ident(
    struct_ident: &Ident,
    trait_ident: &Ident,
    ident: &Ident,
) -> Ident {
    Ident::new(
        &format!(
            "{}_IMPL_TRAIT_{}_{}_FUNCTION_ID",
            struct_ident.to_string().to_uppercase(),
            trait_ident.to_string().to_uppercase(),
            ident.to_string().to_uppercase()
        ),
        ident.span(),
    )
}

pub fn get_internal_trait_impl_function_ident(trait_ident: &Ident, ident: &Ident) -> Ident {
    Ident::new(
        &format!("__trait_call_{trait_ident}_{ident}"),
        trait_ident.span(),
    )
}

pub fn get_path_ident(path: &Path) -> Ident {
    let mut result = String::new();

    for (i, segment) in path.segments.iter().enumerate() {
        let ident = segment.ident.to_string();

        if i > 0 {
            result.push('_');
        }

        result.push_str(&ident);

        match &segment.arguments {
            PathArguments::AngleBracketed(args) => {
                for arg in &args.args {
                    match arg {
                        GenericArgument::Type(ty) => {
                            if let Type::Path(type_path) = ty {
                                let type_ident = get_path_ident(&type_path.path);
                                result.push('_');
                                result.push_str(&type_ident.to_string());
                            } else if let Type::TraitObject(trait_obj) = ty {
                                for bound in &trait_obj.bounds {
                                    if let TypeParamBound::Trait(bound_trait) = bound {
                                        let bound_ident = get_path_ident(&bound_trait.path);
                                        result.push_str("_dyn_");
                                        result.push_str(&bound_ident.to_string());
                                    }
                                }
                            } else {
                                arg.span()
                                    .unwrap()
                                    .error(
                                        "#[turbo_tasks::value_impl] does not support this type \
                                         argument",
                                    )
                                    .emit();
                            }
                        }
                        _ => arg
                            .span()
                            .unwrap()
                            .error("#[turbo_tasks::value_impl] does not support this type argument")
                            .emit(),
                    }
                }
            }
            PathArguments::None => {}
            _ => {
                segment
                    .span()
                    .unwrap()
                    .error("#[turbo_tasks::value_impl] does not support this type argument")
                    .emit();
            }
        }
    }

    Ident::new(&result, path.span())
}

pub fn get_type_ident(ty: &Type) -> Option<Ident> {
    match ty {
        Type::Path(path) => Some(get_path_ident(&path.path)),
        Type::Tuple(tuple) => Some(Ident::new("unit", tuple.span())),
        _ => {
            ty.span()
                .unwrap()
                .error(format!(
                    "#[turbo_tasks::value_impl] does not support the type {}, expected T or \
                     Box<dyn Trait>",
                    ty.to_token_stream()
                ))
                .emit();
            None
        }
    }
}
pub fn get_trait_default_impl_function_ident(trait_ident: &Ident, ident: &Ident) -> Ident {
    Ident::new(
        &format!(
            "{}_DEFAULT_IMPL_{}_FUNCTION",
            trait_ident.to_string().to_uppercase(),
            ident.to_string().to_uppercase()
        ),
        ident.span(),
    )
}

pub fn get_value_type_ident(ident: &Ident) -> Ident {
    Ident::new(
        &format!("{}_VALUE_TYPE", ident.to_string().to_uppercase()),
        ident.span(),
    )
}

pub fn get_value_type_init_ident(ident: &Ident) -> Ident {
    Ident::new(
        &format!("{}_VALUE_TYPE_INIT", ident.to_string().to_uppercase()),
        ident.span(),
    )
}