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
304
305
306
307
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use smol_str::SmolStr;
use std::fmt;
use syn::{parse_quote, spanned::Spanned, Type};

use crate::{
    error::{DiagnosticError, Result, SourceIdSpan},
    source_registry::SourceId,
    typemap::ast::DisplayToTokens,
    SMART_PTR_COPY_TRAIT,
};

#[derive(Debug, Clone)]
pub(crate) struct ForeignClassInfo {
    pub src_id: SourceId,
    pub name: Ident,
    pub methods: Vec<ForeignMethod>,
    pub self_desc: Option<SelfTypeDesc>,
    pub foreign_code: String,
    pub doc_comments: Vec<String>,
    pub derive_list: Vec<String>,
}

/// Two types instead of one, to simplify live to developer
/// For example, it is possible to use `Rc<RefCell<T>>` as constructor
/// return type, and `T` as self type, and we generate all code to convert
/// back and forth pointer to `RefCell<T>>` and `T`
#[derive(Debug, Clone)]
pub(crate) struct SelfTypeDesc {
    pub self_type: Type,
    pub constructor_ret_type: Type,
}

impl ForeignClassInfo {
    pub(crate) fn span(&self) -> Span {
        self.name.span()
    }
    pub(crate) fn self_type_as_ty(&self) -> Type {
        self.self_desc
            .as_ref()
            .map(|x| x.self_type.clone())
            .unwrap_or_else(|| parse_quote! { () })
    }
    /// common for several language binding generator code
    pub(crate) fn validate_class(&self) -> Result<()> {
        let mut has_constructor = false;
        let mut has_methods = false;
        let mut has_static_methods = false;
        for x in &self.methods {
            match x.variant {
                MethodVariant::Constructor => has_constructor = true,
                MethodVariant::Method(_) => has_methods = true,
                MethodVariant::StaticMethod => has_static_methods = true,
            }
        }
        let self_type_is_some = self.self_desc.is_some();
        if !self_type_is_some && has_methods {
            Err(DiagnosticError::new(
                self.src_id,
                self.span(),
                format!("class {} has methods, but no self_type defined", self.name),
            ))
        } else if self_type_is_some && !has_static_methods && !has_constructor && !has_methods {
            Err(DiagnosticError::new(
                self.src_id,
                self.span(),
                format!(
                    "class {} has only self_type, but no methods or constructors",
                    self.name
                ),
            ))
        } else {
            Ok(())
        }
    }
    pub fn copy_derived(&self) -> bool {
        self.derive_list.iter().any(|x| x == "Copy")
    }
    pub fn smart_ptr_copy_derived(&self) -> bool {
        self.derive_list.iter().any(|x| x == SMART_PTR_COPY_TRAIT)
    }
    /// constructor type implements Clone trait
    pub fn clone_derived(&self) -> bool {
        self.derive_list.iter().any(|x| x == "Clone")
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ForeignMethod {
    pub(crate) variant: MethodVariant,
    pub(crate) rust_id: syn::Path,
    pub(crate) fn_decl: FnDecl,
    pub(crate) name_alias: Option<Ident>,
    pub(crate) access: MethodAccess,
    pub(crate) doc_comments: Vec<String>,
    pub(crate) inline_block: Option<syn::Block>,
    pub(crate) unknown_attrs: Vec<String>,
}

#[derive(Debug, Clone)]
pub(crate) enum FnArg {
    SelfArg(Span, SelfTypeVariant),
    Default(NamedArg),
}

impl FnArg {
    pub(crate) fn as_named_arg(&self) -> syn::Result<&NamedArg> {
        match self {
            FnArg::SelfArg(sp, _) => Err(syn::Error::new(*sp, "expect not self argument here")),
            FnArg::Default(ref arg) => Ok(arg),
        }
    }
    pub(crate) fn as_self_arg(&self, src_id: SourceId) -> Result<SelfTypeVariant> {
        match self {
            FnArg::SelfArg(_, var) => Ok(*var),
            FnArg::Default(ref arg) => Err(DiagnosticError::new(
                src_id,
                arg.span,
                "expect self argument here",
            )),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct NamedArg {
    pub name: SmolStr,
    pub span: Span,
    pub ty: syn::Type,
}

#[derive(Debug, Clone)]
pub(crate) struct FnDecl {
    pub(crate) span: Span,
    pub(crate) inputs: Vec<FnArg>,
    pub(crate) output: syn::ReturnType,
}

impl ForeignMethod {
    pub(crate) fn short_name(&self) -> String {
        if let Some(ref name) = self.name_alias {
            name.to_string()
        } else {
            match self.rust_id.segments.len() {
                0 => String::new(),
                n => self.rust_id.segments[n - 1].ident.to_string(),
            }
        }
    }

    pub(crate) fn span(&self) -> Span {
        self.rust_id.span()
    }

    pub(crate) fn is_dummy_constructor(&self) -> bool {
        self.rust_id.segments.is_empty()
    }

    pub(crate) fn arg_names_without_self(&self) -> impl Iterator<Item = &str> {
        let skip = match self.variant {
            MethodVariant::Method(_) => 1,
            _ => 0,
        };
        self.fn_decl
            .inputs
            .iter()
            .skip(skip)
            .map(|x| x.as_named_arg().unwrap().name.as_str())
    }

    pub(crate) fn generate_code_to_call_rust_func(&self) -> String {
        if let Some(ref code_block) = self.inline_block {
            format!("{}", DisplayToTokens(code_block))
        } else {
            let args_names = self
                .arg_names_without_self()
                .fold(String::new(), |mut acc, x| {
                    if !acc.is_empty() {
                        acc.push_str(", ");
                    }
                    acc.push_str(&x);
                    acc
                });
            if let MethodVariant::Method(_) = self.variant {
                format!("{}(this, {})", DisplayToTokens(&self.rust_id), args_names)
            } else {
                format!("{}({})", DisplayToTokens(&self.rust_id), args_names)
            }
        }
    }
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub(crate) enum MethodAccess {
    Private,
    Public,
    Protected,
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub enum MethodVariant {
    Constructor,
    Method(SelfTypeVariant),
    StaticMethod,
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub enum SelfTypeVariant {
    RptrMut,
    Rptr,
    Mut,
    Default,
}

impl From<SelfTypeVariant> for TokenStream {
    fn from(x: SelfTypeVariant) -> TokenStream {
        match x {
            SelfTypeVariant::RptrMut => quote!(&mut self),
            SelfTypeVariant::Rptr => quote!(&self),
            SelfTypeVariant::Mut => quote!(mut self),
            SelfTypeVariant::Default => quote!(self),
        }
    }
}

impl fmt::Display for SelfTypeVariant {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
        use SelfTypeVariant::*;
        f.write_str(match self {
            RptrMut => "&mut self",
            Rptr => "&self",
            Mut => "mut self",
            Default => "self",
        })
    }
}

impl SelfTypeVariant {
    pub(crate) fn is_read_only(self) -> bool {
        match self {
            SelfTypeVariant::RptrMut | SelfTypeVariant::Mut => false,
            SelfTypeVariant::Default | SelfTypeVariant::Rptr => true,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ForeignEnumInfo {
    pub(crate) src_id: SourceId,
    pub(crate) name: Ident,
    pub(crate) items: Vec<ForeignEnumItem>,
    pub(crate) doc_comments: Vec<String>,
}

impl ForeignEnumInfo {
    pub(crate) fn span(&self) -> Span {
        self.name.span()
    }
}

#[derive(Debug, Clone)]
pub(crate) struct ForeignEnumItem {
    pub(crate) name: Ident,
    pub(crate) rust_name: syn::Path,
    pub(crate) doc_comments: Vec<String>,
}

pub(crate) struct ForeignInterface {
    pub(crate) src_id: SourceId,
    pub(crate) name: Ident,
    pub(crate) self_type: syn::TypeTraitObject,
    pub(crate) doc_comments: Vec<String>,
    pub(crate) items: Vec<ForeignInterfaceMethod>,
}

impl ForeignInterface {
    pub(crate) fn span(&self) -> Span {
        self.name.span()
    }
    pub(crate) fn src_id_span(&self) -> SourceIdSpan {
        (self.src_id, self.name.span())
    }
}

pub(crate) struct ForeignInterfaceMethod {
    pub(crate) name: Ident,
    pub(crate) rust_name: syn::Path,
    pub(crate) fn_decl: FnDecl,
    pub(crate) doc_comments: Vec<String>,
}

impl ForeignInterfaceMethod {
    pub(crate) fn arg_names_without_self(&self) -> impl Iterator<Item = &str> {
        self.fn_decl
            .inputs
            .iter()
            .skip(1)
            .map(|x| x.as_named_arg().unwrap().name.as_str())
    }
}

pub(crate) enum ItemToExpand {
    Class(Box<ForeignClassInfo>),
    Interface(ForeignInterface),
    Enum(ForeignEnumInfo),
}