summaryrefslogtreecommitdiffstats
path: root/src/bindgen/ir/generic_path.rs
blob: ef14890ab14eaa7245dd4192751a368240222a6f (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::io::Write;
use std::ops::Deref;

use syn::ext::IdentExt;

use crate::bindgen::cdecl;
use crate::bindgen::config::{Config, Language};
use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver};
use crate::bindgen::ir::{ConstExpr, Path, Type};
use crate::bindgen::utilities::IterHelpers;
use crate::bindgen::writer::{Source, SourceWriter};

#[derive(Debug, Clone)]
pub enum GenericParamType {
    Type,
    Const(Type),
}

#[derive(Debug, Clone)]
pub struct GenericParam {
    name: Path,
    ty: GenericParamType,
}

impl GenericParam {
    pub fn new_type_param(name: &str) -> Self {
        GenericParam {
            name: Path::new(name),
            ty: GenericParamType::Type,
        }
    }

    pub fn load(param: &syn::GenericParam) -> Result<Option<Self>, String> {
        match *param {
            syn::GenericParam::Type(syn::TypeParam { ref ident, .. }) => Ok(Some(GenericParam {
                name: Path::new(ident.unraw().to_string()),
                ty: GenericParamType::Type,
            })),

            syn::GenericParam::Lifetime(_) => Ok(None),

            syn::GenericParam::Const(syn::ConstParam {
                ref ident, ref ty, ..
            }) => match Type::load(ty)? {
                None => {
                    // A type that evaporates, like PhantomData.
                    Err(format!("unsupported const generic type: {:?}", ty))
                }
                Some(ty) => Ok(Some(GenericParam {
                    name: Path::new(ident.unraw().to_string()),
                    ty: GenericParamType::Const(ty),
                })),
            },
        }
    }

    pub fn name(&self) -> &Path {
        &self.name
    }
}

#[derive(Default, Debug, Clone)]
pub struct GenericParams(pub Vec<GenericParam>);

impl GenericParams {
    pub fn load(generics: &syn::Generics) -> Result<Self, String> {
        let mut params = vec![];
        for param in &generics.params {
            if let Some(p) = GenericParam::load(param)? {
                params.push(p);
            }
        }

        Ok(GenericParams(params))
    }

    /// Associate each parameter with an argument.
    pub fn call<'out>(
        &'out self,
        item_name: &str,
        arguments: &'out [GenericArgument],
    ) -> Vec<(&'out Path, &'out GenericArgument)> {
        assert!(self.len() > 0, "{} is not generic", item_name);
        assert!(
            self.len() == arguments.len(),
            "{} has {} params but is being instantiated with {} values",
            item_name,
            self.len(),
            arguments.len(),
        );
        self.iter()
            .map(|param| param.name())
            .zip(arguments.iter())
            .collect()
    }

    fn write_internal<F: Write>(
        &self,
        config: &Config,
        out: &mut SourceWriter<F>,
        with_default: bool,
    ) {
        if !self.0.is_empty() && config.language == Language::Cxx {
            out.write("template<");
            for (i, item) in self.0.iter().enumerate() {
                if i != 0 {
                    out.write(", ");
                }
                match item.ty {
                    GenericParamType::Type => {
                        write!(out, "typename {}", item.name);
                        if with_default {
                            write!(out, " = void");
                        }
                    }
                    GenericParamType::Const(ref ty) => {
                        cdecl::write_field(out, ty, item.name.name(), config);
                        if with_default {
                            write!(out, " = 0");
                        }
                    }
                }
            }
            out.write(">");
            out.new_line();
        }
    }

    pub fn write_with_default<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
        self.write_internal(config, out, true);
    }
}

impl Deref for GenericParams {
    type Target = [GenericParam];

    fn deref(&self) -> &[GenericParam] {
        &self.0
    }
}

impl Source for GenericParams {
    fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
        self.write_internal(config, out, false);
    }
}

/// A (non-lifetime) argument passed to a generic, either a type or a constant expression.
///
/// Note: Both arguments in a type like `Array<T, N>` are represented as
/// `GenericArgument::Type`s, even if `N` is actually the name of a const. This
/// is a consequence of `syn::GenericArgument` doing the same thing.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum GenericArgument {
    Type(Type),
    Const(ConstExpr),
}

impl GenericArgument {
    pub fn specialize(&self, mappings: &[(&Path, &GenericArgument)]) -> GenericArgument {
        match *self {
            GenericArgument::Type(ref ty) => {
                if let Type::Path(ref path) = *ty {
                    if path.is_single_identifier() {
                        // See note on `GenericArgument` above: `ty` may
                        // actually be the name of a const. Check for that now.
                        for &(name, value) in mappings {
                            if *name == path.path {
                                return value.clone();
                            }
                        }
                    }
                }
                GenericArgument::Type(ty.specialize(mappings))
            }
            GenericArgument::Const(ref expr) => GenericArgument::Const(expr.clone()),
        }
    }

    pub fn rename_for_config(&mut self, config: &Config, generic_params: &GenericParams) {
        match *self {
            GenericArgument::Type(ref mut ty) => ty.rename_for_config(config, generic_params),
            GenericArgument::Const(ref mut expr) => expr.rename_for_config(config),
        }
    }
}

impl Source for GenericArgument {
    fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
        match *self {
            GenericArgument::Type(ref ty) => ty.write(config, out),
            GenericArgument::Const(ref expr) => expr.write(config, out),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GenericPath {
    path: Path,
    export_name: String,
    generics: Vec<GenericArgument>,
    ctype: Option<DeclarationType>,
}

impl GenericPath {
    pub fn new(path: Path, generics: Vec<GenericArgument>) -> Self {
        let export_name = path.name().to_owned();
        Self {
            path,
            export_name,
            generics,
            ctype: None,
        }
    }

    pub fn self_path() -> Self {
        Self::new(Path::new("Self"), vec![])
    }

    pub fn replace_self_with(&mut self, self_ty: &Path) {
        if self.path.replace_self_with(self_ty) {
            self.export_name = self_ty.name().to_owned();
        }
        // Caller deals with generics.
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn generics(&self) -> &[GenericArgument] {
        &self.generics
    }

    pub fn generics_mut(&mut self) -> &mut [GenericArgument] {
        &mut self.generics
    }

    pub fn ctype(&self) -> Option<&DeclarationType> {
        self.ctype.as_ref()
    }

    pub fn name(&self) -> &str {
        self.path.name()
    }

    pub fn export_name(&self) -> &str {
        &self.export_name
    }

    pub fn is_single_identifier(&self) -> bool {
        self.generics.is_empty()
    }

    pub fn rename_for_config(&mut self, config: &Config, generic_params: &GenericParams) {
        for generic in &mut self.generics {
            generic.rename_for_config(config, generic_params);
        }
        if !generic_params.iter().any(|param| param.name == self.path) {
            config.export.rename(&mut self.export_name);
        }
    }

    pub fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) {
        self.ctype = resolver.type_for(&self.path);
    }

    pub fn load(path: &syn::Path) -> Result<Self, String> {
        assert!(
            !path.segments.is_empty(),
            "{:?} doesn't have any segments",
            path
        );
        let last_segment = path.segments.last().unwrap();
        let name = last_segment.ident.unraw().to_string();

        let path = Path::new(name);
        let phantom_data_path = Path::new("PhantomData");
        if path == phantom_data_path {
            return Ok(Self::new(path, Vec::new()));
        }

        let generics = match last_segment.arguments {
            syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
                ref args,
                ..
            }) => args.iter().try_skip_map(|x| match *x {
                syn::GenericArgument::Type(ref x) => Ok(Type::load(x)?.map(GenericArgument::Type)),
                syn::GenericArgument::Lifetime(_) => Ok(None),
                syn::GenericArgument::Const(ref x) => {
                    Ok(Some(GenericArgument::Const(ConstExpr::load(x)?)))
                }
                _ => Err(format!("can't handle generic argument {:?}", x)),
            })?,
            syn::PathArguments::Parenthesized(_) => {
                return Err("Path contains parentheses.".to_owned());
            }
            _ => Vec::new(),
        };

        Ok(Self::new(path, generics))
    }
}