Skip to main content

leo_ast/composite/
mod.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17pub mod member;
18pub use member::*;
19
20use crate::{ConstParameter, Identifier, Indent, Mode, Node, NodeID, Type};
21use leo_span::{Span, Symbol};
22
23use itertools::Itertools;
24use serde::{Deserialize, Serialize};
25use std::fmt;
26
27use snarkvm::{
28    console::program::{RecordType, StructType},
29    prelude::{
30        EntryType::{Constant, Private, Public},
31        Network,
32    },
33};
34
35/// A composite type definition, e.g., `struct Foo { my_field: Bar }` and `record Token { owner: address, amount: u64}`.
36///
37/// Type identity is decided by the full path including `identifier`,
38/// as the record is nominal, not structural.
39/// The fields are named so `struct Foo(u8, u16)` is not allowed.
40#[derive(Clone, Debug, Serialize, Deserialize)]
41pub struct Composite {
42    /// The name of the type in the type system in this module.
43    pub identifier: Identifier,
44    /// The composite's const parameters.
45    pub const_parameters: Vec<ConstParameter>,
46    /// The fields, constant variables, and functions of this composite.
47    pub members: Vec<Member>,
48    /// Was this a `record Foo { ... }`?
49    /// If so, it wasn't a composite.
50    pub is_record: bool,
51    /// The entire span of the composite definition.
52    pub span: Span,
53    /// The ID of the node.
54    pub id: NodeID,
55}
56
57impl PartialEq for Composite {
58    fn eq(&self, other: &Self) -> bool {
59        self.identifier == other.identifier
60    }
61}
62
63impl Eq for Composite {}
64
65impl Composite {
66    /// Returns the composite name as a Symbol.
67    pub fn name(&self) -> Symbol {
68        self.identifier.name
69    }
70
71    pub fn from_external_record<N: Network>(input: &RecordType<N>, program: Symbol) -> Self {
72        let mut members = Vec::with_capacity(input.entries().len() + 1);
73        members.push(Member {
74            mode: if input.owner().is_private() { Mode::Public } else { Mode::Private },
75            identifier: Identifier::new(Symbol::intern("owner"), Default::default()),
76            type_: Type::Address,
77            span: Default::default(),
78            id: Default::default(),
79        });
80        members.extend(input.entries().iter().map(|(id, entry)| Member {
81            mode: if input.owner().is_public() { Mode::Public } else { Mode::Private },
82            identifier: Identifier::from(id),
83            type_: match entry {
84                Public(t) => Type::from_snarkvm(t, program),
85                Private(t) => Type::from_snarkvm(t, program),
86                Constant(t) => Type::from_snarkvm(t, program),
87            },
88            span: Default::default(),
89            id: Default::default(),
90        }));
91        Self {
92            identifier: Identifier::from(input.name()),
93            const_parameters: Vec::new(),
94            members,
95            is_record: true,
96            span: Default::default(),
97            id: Default::default(),
98        }
99    }
100
101    pub fn from_snarkvm<N: Network>(input: &StructType<N>, program: Symbol) -> Self {
102        Self {
103            identifier: Identifier::from(input.name()),
104            const_parameters: Vec::new(),
105            members: input
106                .members()
107                .iter()
108                .map(|(id, type_)| Member {
109                    mode: Mode::None,
110                    identifier: Identifier::from(id),
111                    type_: Type::from_snarkvm(type_, program),
112                    span: Default::default(),
113                    id: Default::default(),
114                })
115                .collect(),
116            is_record: false,
117            span: Default::default(),
118            id: Default::default(),
119        }
120    }
121}
122
123impl fmt::Display for Composite {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        f.write_str(if self.is_record { "record" } else { "struct" })?;
126        write!(f, " {}", self.identifier)?;
127        if !self.const_parameters.is_empty() {
128            write!(f, "::[{}]", self.const_parameters.iter().format(", "))?;
129        }
130        writeln!(f, " {{")?;
131
132        for field in self.members.iter() {
133            writeln!(f, "{},", Indent(field))?;
134        }
135        write!(f, "}}")
136    }
137}
138
139crate::simple_node_impl!(Composite);