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, ProgramId, TypeKind, TypeNode};
21use leo_span::{Span, Symbol};
22
23use itertools::Itertools;
24use serde::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)]
41pub struct Composite {
42    /// Whether the `export` keyword was written on this composite. `None` when the
43    /// concept doesn't apply (records, program-block composites, and structs
44    /// imported from another unit, all always reachable).
45    pub is_exported: Option<bool>,
46    /// The name of the type in the type system in this module.
47    pub identifier: Identifier,
48    /// The composite's const parameters.
49    pub const_parameters: Vec<ConstParameter>,
50    /// The fields, constant variables, and functions of this composite.
51    pub members: Vec<Member>,
52    /// Was this a `record Foo { ... }`?
53    /// If so, it wasn't a composite.
54    pub is_record: bool,
55    /// The entire span of the composite definition.
56    pub span: Span,
57    /// The ID of the node.
58    pub id: NodeID,
59}
60
61impl PartialEq for Composite {
62    fn eq(&self, other: &Self) -> bool {
63        self.identifier == other.identifier
64    }
65}
66
67impl Eq for Composite {}
68
69impl Composite {
70    /// Returns the composite name as a Symbol.
71    pub fn name(&self) -> Symbol {
72        self.identifier.name
73    }
74
75    // No interner in scope at disassembly time; stub types get their canonical handle when
76    // the frontend re-interns them during stub merge.
77    pub fn from_external_record<N: Network>(input: &RecordType<N>, program_id: ProgramId) -> Self {
78        let mut members = Vec::with_capacity(input.entries().len() + 1);
79        members.push(Member {
80            mode: if input.owner().is_public() { Mode::Public } else { Mode::Private },
81            identifier: Identifier::new(Symbol::intern("owner"), Default::default()),
82            type_: TypeNode::unchecked(TypeKind::Address, Span::default()),
83            span: Default::default(),
84            id: Default::default(),
85        });
86        members.extend(input.entries().iter().map(|(id, entry)| {
87            let (mode, type_) = match entry {
88                Public(type_) => (Mode::Public, type_),
89                Private(type_) => (Mode::Private, type_),
90                Constant(type_) => (Mode::Constant, type_),
91            };
92            Member {
93                mode,
94                identifier: Identifier::from(id),
95                type_: TypeNode::unchecked(TypeKind::from_snarkvm(type_, program_id), Span::default()),
96                span: Default::default(),
97                id: Default::default(),
98            }
99        }));
100        Self {
101            is_exported: None,
102            identifier: Identifier::from(input.name()),
103            const_parameters: Vec::new(),
104            members,
105            is_record: true,
106            span: Default::default(),
107            id: Default::default(),
108        }
109    }
110
111    // See `from_external_record` for the `unchecked` rationale.
112    pub fn from_snarkvm<N: Network>(input: &StructType<N>, program: ProgramId) -> Self {
113        Self {
114            is_exported: None,
115            identifier: Identifier::from(input.name()),
116            const_parameters: Vec::new(),
117            members: input
118                .members()
119                .iter()
120                .map(|(id, type_)| Member {
121                    mode: Mode::None,
122                    identifier: Identifier::from(id),
123                    type_: TypeNode::unchecked(TypeKind::from_snarkvm(type_, program), Span::default()),
124                    span: Default::default(),
125                    id: Default::default(),
126                })
127                .collect(),
128            is_record: false,
129            span: Default::default(),
130            id: Default::default(),
131        }
132    }
133}
134
135impl fmt::Display for Composite {
136    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137        if self.is_exported == Some(true) {
138            f.write_str("export ")?;
139        }
140        f.write_str(if self.is_record { "record" } else { "struct" })?;
141        write!(f, " {}", self.identifier)?;
142        if !self.const_parameters.is_empty() {
143            write!(f, "::[{}]", self.const_parameters.iter().format(", "))?;
144        }
145        writeln!(f, " {{")?;
146
147        for field in self.members.iter() {
148            writeln!(f, "{},", Indent(field))?;
149        }
150        write!(f, "}}")
151    }
152}
153
154crate::simple_node_impl!(Composite);