diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 166c6479b15..797b8f3828a 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -130,6 +130,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "module_has_named_attribute" => module_has_named_attribute(self, arguments, location), "module_is_contract" => module_is_contract(self, arguments, location), "module_name" => module_name(interner, arguments, location), + "module_structs" => module_structs(self, arguments, location), "modulus_be_bits" => modulus_be_bits(interner, arguments, location), "modulus_be_bytes" => modulus_be_bytes(interner, arguments, location), "modulus_le_bits" => modulus_le_bits(interner, arguments, location), @@ -2294,6 +2295,30 @@ fn module_functions( Ok(Value::Slice(func_ids, slice_type)) } +// fn structs(self) -> [StructDefinition] +fn module_structs( + interpreter: &Interpreter, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let self_argument = check_one_argument(arguments, location)?; + let module_id = get_module(self_argument)?; + let module_data = interpreter.elaborator.get_module(module_id); + let struct_ids = module_data + .type_definitions() + .filter_map(|module_def_id| { + if let ModuleDefId::TypeId(id) = module_def_id { + Some(Value::StructDefinition(id)) + } else { + None + } + }) + .collect(); + + let slice_type = Type::Slice(Box::new(Type::Quoted(QuotedType::StructDefinition))); + Ok(Value::Slice(struct_ids, slice_type)) +} + // fn has_named_attribute(self, name: Quoted) -> bool fn module_has_named_attribute( interpreter: &Interpreter, diff --git a/docs/docs/noir/standard_library/meta/module.md b/docs/docs/noir/standard_library/meta/module.md index de042760d51..1bc11e725a3 100644 --- a/docs/docs/noir/standard_library/meta/module.md +++ b/docs/docs/noir/standard_library/meta/module.md @@ -16,17 +16,11 @@ Adds a top-level item (a function, a struct, a global, etc.) to the module. Adding multiple items in one go is also valid if the `Quoted` value has multiple items in it. Note that the items are type-checked as if they are inside the module they are being added to. -### name - -#include_code name noir_stdlib/src/meta/module.nr rust - -Returns the name of the module. - ### functions #include_code functions noir_stdlib/src/meta/module.nr rust -Returns each function in the module. +Returns each function defined in the module. ### has_named_attribute @@ -39,3 +33,15 @@ Returns true if this module has a custom attribute with the given name. #include_code is_contract noir_stdlib/src/meta/module.nr rust `true` if this module is a contract module (was declared via `contract foo { ... }`). + +### name + +#include_code name noir_stdlib/src/meta/module.nr rust + +Returns the name of the module. + +### structs + +#include_code structs noir_stdlib/src/meta/module.nr rust + +Returns each struct defined in the module. diff --git a/noir_stdlib/src/meta/module.nr b/noir_stdlib/src/meta/module.nr index 6f936bf4c57..5a7adab0d27 100644 --- a/noir_stdlib/src/meta/module.nr +++ b/noir_stdlib/src/meta/module.nr @@ -10,17 +10,22 @@ impl Module { // docs:end:has_named_attribute #[builtin(module_is_contract)] -// docs:start:is_contract + // docs:start:is_contract comptime fn is_contract(self) -> bool {} // docs:end:is_contract #[builtin(module_functions)] -// docs:start:functions + // docs:start:functions comptime fn functions(self) -> [FunctionDefinition] {} // docs:end:functions + #[builtin(module_structs)] + // docs:start:structs + comptime fn structs(self) -> [StructDefinition] {} + // docs:end:structs + #[builtin(module_name)] -// docs:start:name + // docs:start:name comptime fn name(self) -> Quoted {} // docs:end:name } diff --git a/test_programs/compile_success_empty/comptime_module/src/main.nr b/test_programs/compile_success_empty/comptime_module/src/main.nr index c3ba7ba4643..21296cce056 100644 --- a/test_programs/compile_success_empty/comptime_module/src/main.nr +++ b/test_programs/compile_success_empty/comptime_module/src/main.nr @@ -3,6 +3,8 @@ mod foo { #![some_attribute] pub fn x() {} pub fn y() {} + + struct Struct1 {} } contract bar {} @@ -74,6 +76,10 @@ fn main() { assert_eq(foo.functions().len(), 2); assert_eq(bar.functions().len(), 0); + // Check Module::structs + assert_eq(foo.structs().len(), 1); + assert_eq(bar.structs().len(), 0); + // Check Module::name assert_eq(foo.name(), quote { foo }); assert_eq(bar.name(), quote { bar });