Skip to content

Commit

Permalink
Merge f84bca0 into 1b72a17
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Sep 6, 2024
2 parents 1b72a17 + f84bca0 commit 6adb490
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
struct_def_has_named_attribute(interner, arguments, location)
}
"struct_def_module" => struct_def_module(self, arguments, location),
"struct_def_name" => struct_def_name(interner, arguments, location),
"struct_def_set_fields" => struct_def_set_fields(interner, arguments, location),
"to_le_radix" => to_le_radix(arguments, return_type, location),
"trait_constraint_eq" => trait_constraint_eq(interner, arguments, location),
Expand Down Expand Up @@ -423,6 +424,20 @@ fn struct_def_module(
Ok(Value::ModuleDefinition(parent))
}

// fn name(self) -> Quoted
fn struct_def_name(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let struct_id = get_struct(self_argument)?;
let the_struct = interner.get_struct(struct_id);

let name = Token::Ident(the_struct.borrow().name.to_string());
Ok(Value::Quoted(Rc::new(vec![name])))
}

/// fn set_fields(self, new_fields: [(Quoted, Type)]) {}
/// Returns (name, type) pairs of each field of this StructDefinition
fn struct_def_set_fields(
Expand Down
9 changes: 9 additions & 0 deletions docs/docs/noir/standard_library/meta/struct_def.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Returns true if this struct has a custom attribute with the given name.

Returns the module where the struct is defined.

### name

#include_code name noir_stdlib/src/meta/struct_def.nr rust

Returns the name of this struct

Note that the returned quoted value will be just the struct name, it will
not be the full path to the struct, nor will it include any generics.

### set_fields

#include_code set_fields noir_stdlib/src/meta/struct_def.nr rust
Expand Down
5 changes: 5 additions & 0 deletions noir_stdlib/src/meta/struct_def.nr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl StructDefinition {
fn module(self) -> Module {}
// docs:end:module

#[builtin(struct_def_name)]
// docs:start:name
fn name(self) -> Quoted {}
// docs:end:name

/// Sets the fields of this struct to the given fields list.
/// All existing fields of the struct will be overridden with the given fields.
/// Each element of the fields list corresponds to the name and type of a field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ comptime fn my_comptime_fn(typ: StructDefinition) {
let _ = typ.as_type();
assert_eq(typ.generics().len(), 3);
assert_eq(typ.fields().len(), 2);
assert_eq(typ.name(), quote { MyType });
}

comptime fn mutate_struct_fields(s: StructDefinition) {
Expand Down

0 comments on commit 6adb490

Please sign in to comment.