holium_backend/
ast.rs

1//! Contains all structures that can be parsed from a `TokenStream`. They will be used when generating
2//! code
3
4use proc_macro2::{Ident, Span};
5use syn;
6
7/// An abstract syntax tree representing a rust program. Contains
8/// extra information for joining up this rust code with javascript.
9#[cfg_attr(feature = "extra-traits", derive(Debug))]
10#[derive(Default, Clone)]
11pub struct Program {
12    /// rust func
13    pub exports: Vec<Export>,
14    /// rust structs
15    pub structs: Vec<Struct>,
16}
17
18impl Program {
19    /// Returns true if the Program is empty
20    pub fn is_empty(&self) -> bool {
21        self.exports.is_empty()
22    }
23}
24
25/// A rust to js interface. Allows interaction with rust objects/functions
26/// from javascript.
27#[cfg_attr(feature = "extra-traits", derive(Debug))]
28#[derive(Clone)]
29pub struct Export {
30    /// The rust function
31    pub function: Function,
32    /// The kind (static, named, regular)
33    pub method_kind: MethodKind,
34    /// The struct name, in Rust, this is attached to
35    pub rust_class: Option<Ident>,
36    /// The name of the rust function/method on the rust source code
37    pub rust_name: Ident,
38}
39
40/// The type of a method
41#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
42#[derive(Clone)]
43pub enum MethodKind {
44    /// Any other kind of method
45    Operation(Operation),
46}
47
48/// The operation performed by a class method
49#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
50#[derive(Clone)]
51pub struct Operation {
52    /// Whether this method is static
53    pub is_static: bool,
54    /// The internal kind of this Operation
55    pub kind: OperationKind,
56}
57
58/// The kind of operation performed by a method
59#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
60#[derive(Clone)]
61pub enum OperationKind {
62    /// A standard method, nothing special
63    Regular,
64}
65
66/// Information about a function being imported or exported
67#[cfg_attr(feature = "extra-traits", derive(Debug))]
68#[derive(Clone)]
69pub struct Function {
70    /// The name of the function
71    pub name: String,
72    /// The arguments to the function
73    pub arguments: Vec<syn::PatType>,
74    /// The return type of the function, if provided
75    pub ret: Option<syn::Type>,
76}
77
78/// Information about a Struct being exported
79#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
80#[derive(Clone)]
81pub struct Struct {
82    /// The name of the struct in Rust code
83    pub rust_name: Ident,
84    /// The name of the struct for Holium
85    pub name: String,
86    /// All the fields of this struct to export
87    pub fields: Vec<StructField>,
88}
89
90/// The field of a struct
91#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
92#[derive(Clone)]
93pub struct StructField {
94    /// The name of the field in Rust code
95    pub rust_name: syn::Member,
96    /// The name of the field in code
97    pub name: String,
98    /// The name of the struct this field is part of
99    pub struct_name: Ident,
100    /// The type of this field
101    pub ty: syn::Type,
102}
103
104impl Export {
105    /// Generate unique function name for our exported Rust function. For a function named "main" the
106    /// resulting name will be "__holium_bindgen_generated_main"
107    pub(crate) fn rust_symbol(&self) -> Ident {
108        let mut generated_name = String::from("__holium_bindgen_generated");
109        generated_name.push_str("_");
110        generated_name.push_str(&self.function.name.to_string());
111        Ident::new(&generated_name, Span::call_site())
112    }
113
114    /// This is the name of the shim function that gets exported and takes the raw
115    /// ABI form of its arguments and converts them back into their normal,
116    /// "high level" form before calling the actual function.
117    pub(crate) fn export_name(&self) -> String {
118        self.function.name.to_string()
119    }
120}