/ compiler / ast / src / program / program_scope.rs
program_scope.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  //! A ADL program scope consists of const, composite, function, and mapping definitions.
18  
19  use crate::{Composite, ConstDeclaration, Constructor, Function, Indent, Mapping, ProgramId, StorageVariable, Stub};
20  
21  use adl_span::{Span, Symbol};
22  use serde::{Deserialize, Serialize};
23  use std::fmt;
24  
25  /// Stores the ADL program scope abstract syntax tree.
26  #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
27  pub struct ProgramScope {
28      /// The program id of the program scope.
29      pub program_id: ProgramId,
30      /// A vector of const definitions.
31      pub consts: Vec<(Symbol, ConstDeclaration)>,
32      /// A vector of composite definitions.
33      pub composites: Vec<(Symbol, Composite)>,
34      /// A vector of mapping definitions.
35      pub mappings: Vec<(Symbol, Mapping)>,
36      /// A vector of storage variable definitions.
37      pub storage_variables: Vec<(Symbol, StorageVariable)>,
38      /// A vector of function definitions.
39      pub functions: Vec<(Symbol, Function)>,
40      /// An optional constructor.
41      pub constructor: Option<Constructor>,
42      /// The span associated with the program scope.
43      pub span: Span,
44  }
45  
46  impl From<Stub> for ProgramScope {
47      fn from(stub: Stub) -> Self {
48          Self {
49              program_id: stub.stub_id,
50              consts: stub.consts,
51              composites: stub.composites,
52              mappings: stub.mappings,
53              storage_variables: Vec::new(), // stubs don't have storage variables
54              functions: stub
55                  .functions
56                  .into_iter()
57                  .map(|(symbol, function)| (symbol, Function::from(function)))
58                  .collect(),
59              // A program scope constructed from a stub does not need a constructor, since they are not externally callable.
60              constructor: None,
61              span: stub.span,
62          }
63      }
64  }
65  
66  impl fmt::Display for ProgramScope {
67      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68          writeln!(f, "program {} {{", self.program_id)?;
69          for (_, const_decl) in self.consts.iter() {
70              writeln!(f, "{};", Indent(const_decl))?;
71          }
72          if let Some(constructor) = &self.constructor {
73              writeln!(f, "{}", Indent(constructor))?;
74          }
75          for (_, composite_) in self.composites.iter() {
76              writeln!(f, "{}", Indent(composite_))?;
77          }
78          for (_, mapping) in self.mappings.iter() {
79              writeln!(f, "{};", Indent(mapping))?;
80          }
81          for (_, function) in self.functions.iter() {
82              writeln!(f, "{}", Indent(function))?;
83          }
84          write!(f, "}}")
85      }
86  }