Skip to content

Commit

Permalink
feat: Add StructDefinition::name (#5960)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #5958

## Summary\*

Adds `StructDefinition::name`

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [x] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Sep 6, 2024
1 parent 1b72a17 commit 102ebe3
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 102ebe3

Please sign in to comment.