Debian Codegraph @

third_party/rust/uniffi_internal_macros/src/ir/ast.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 //! AST nodes and code to parse them from syn
6
7 use proc_macro2::Literal;
8 // Use IndexMap for mappings. This preserves the field order and makes the diffs look nicer.
9 use syn::{Generics, Ident, Type, Visibility};
10
11 /// Struct/Enum node in the IR
12 #[derive(Clone)]
13 #[allow(dead_code)]
14 pub struct Node {
15 pub attrs: Attributes,
16 pub vis: Visibility,
17 pub ident: Ident,
18 pub generics: Generics,
19 pub def: NodeDef,
20 }
21
22 #[derive(Clone)]
23 pub enum NodeDef {
24 Struct(Struct),
25 Enum(Vec<Variant>),
26 }
27
28 pub enum NodeKind<'a> {
29 Struct {
30 type_name: &'a Ident,
31 },
32 Variant {
33 type_name: &'a Ident,
34 variant_name: &'a Ident,
35 },
36 }
37
38 #[derive(Clone, Default)]
39 pub struct Attributes {
40 pub from: Option<Ident>,
41 pub wraps: bool,
42 }
43
44 #[derive(Clone)]
45 #[allow(dead_code)]
46 pub struct Struct {
47 pub fields: Fields,
48 pub wraps_field: Option<Field>,
49 }
50
51 #[derive(Clone)]
52 #[allow(dead_code)]
53 pub struct Variant {
54 pub attrs: Attributes,
55 pub vis: Visibility,
56 pub ident: Ident,
57 pub fields: Fields,
58 pub discriminant: Option<Literal>,
59 }
60
61 #[derive(Clone)]
62 pub enum Fields {
63 Unit,
64 Named(Vec<Field>),
65 Unnamed(Vec<Field>),
66 }
67
68 #[derive(Clone)]
69 #[allow(dead_code)]
70 pub struct Field {
71 pub attrs: Attributes,
72 pub vis: Visibility,
73 pub ident: Option<Ident>,
74 pub ty: Type,
75 /// Variable name used it patterns, this will be `ident` for named fields and `var{idx}` for
76 /// unnamed fields.
77 pub var_name: Ident,
78 }
79
80 impl Node {
81 pub fn prev_node_ident(&self) -> &Ident {
82 self.attrs.from.as_ref().unwrap_or(&self.ident)
83 }
84 }
85
86 impl NodeKind<'_> {
87 pub fn field_name(&self, field: &Field) -> String {
88 match (self, &field.ident) {
89 (NodeKind::Struct { .. }, Some(name)) => format!(".{name}"),
90 (NodeKind::Struct { .. }, None) => format!(
91 ".{}",
92 field.var_name.to_string().strip_prefix("var").unwrap()
93 ),
94 (NodeKind::Variant { variant_name, .. }, Some(name)) => {
95 format!(".{variant_name}::{name}")
96 }
97 (NodeKind::Variant { variant_name, .. }, None) => format!(
98 ".{variant_name}::{}",
99 field.var_name.to_string().strip_prefix("var").unwrap()
100 ),
101 }
102 }
103 }
104
105 impl Variant {
106 pub fn prev_node_ident(&self) -> &Ident {
107 self.attrs.from.as_ref().unwrap_or(&self.ident)
108 }
109 }
110
111 impl Fields {
112 pub fn iter(&self) -> impl Iterator<Item = &Field> {
113 match self {
114 Self::Named(fields) => fields.iter(),
115 Self::Unnamed(fields) => fields.iter(),
116 Self::Unit => [].iter(),
117 }
118 }
119
120 pub fn var_names(&self) -> impl Iterator<Item = &Ident> {
121 self.iter().map(|f| &f.var_name)
122 }
123
124 pub fn not_named(&self) -> bool {
125 !matches!(self, Fields::Named(_))
126 }
127 }
128
129 impl Field {
130 pub fn prev_node_ident(&self) -> Option<&Ident> {
131 self.attrs.from.as_ref().or(self.ident.as_ref())
132 }
133 }