leo_passes/code_generation/
type_.rs1use super::*;
18
19use leo_ast::{CompositeType, Location, Mode, Type};
20
21impl CodeGeneratingVisitor<'_> {
22 pub fn visit_type(input: &Type) -> String {
23 match input {
24 Type::Address
25 | Type::Field
26 | Type::Group
27 | Type::Scalar
28 | Type::Signature
29 | Type::String
30 | Type::Future(..)
31 | Type::Identifier(..)
32 | Type::Integer(..) => format!("{input}"),
33 Type::Composite(CompositeType { path, .. }) => {
34 Self::legalize_path(&path.absolute_path()).expect("path format cannot be legalized at this point")
35 }
36 Type::Boolean => {
37 "boolean".into()
39 }
40 Type::Array(array_type) => {
41 format!(
42 "[{}; {}u32]",
43 Self::visit_type(array_type.element_type()),
44 array_type.length.as_u32().expect("length should be known at this point")
45 )
46 }
47 Type::Optional(_) => {
48 panic!("Optional types are not supported at this phase of compilation")
49 }
50 Type::Mapping(_) => {
51 panic!("Mapping types are not supported at this phase of compilation")
52 }
53 Type::Tuple(_) => {
54 panic!("Tuple types should not be visited at this phase of compilation")
55 }
56 Type::Vector(_) => {
57 panic!("Vector types should not be visited at this phase of compilation")
58 }
59 Type::Numeric => panic!("`Numeric` types should not exist at this phase of compilation"),
60 Type::Err => panic!("Error types should not exist at this phase of compilation"),
61 Type::Unit => panic!("Unit types are not supported at this phase of compilation"),
62 }
63 }
64
65 pub fn visit_type_with_visibility(&self, type_: &Type, visibility: Mode) -> String {
66 if let Type::Composite(composite) = type_ {
68 let name = composite.path.absolute_path();
69 let this_program_name = self.program_id.unwrap().name.name;
70 let program_name = composite.program.unwrap_or(this_program_name);
71 if self.state.symbol_table.lookup_record(&Location::new(program_name, name.to_vec())).is_some() {
72 let [record_name] = &name[..] else {
73 panic!("Absolute paths to records can only have a single segment at this stage.")
74 };
75 if program_name == this_program_name {
76 return format!("{record_name}.record");
77 } else {
78 return format!("{program_name}.aleo/{record_name}.record");
79 }
80 }
81 }
82
83 if let Mode::None = visibility {
84 Self::visit_type(type_)
85 } else {
86 format!("{}.{visibility}", Self::visit_type(type_))
87 }
88 }
89}