-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: let
add_definition_location
take a Location (noir-lang/noir#…
…7185) fix(LSP): correct signature for assert and assert_eq (noir-lang/noir#7184) chore(experimental): Prevent enum panics by returning Options where possible instead of panicking (noir-lang/noir#7180) feat(experimental): Construct enum variants in expressions (noir-lang/noir#7174) feat: add `noir-inspector` (noir-lang/noir#7136) fix: ensure canonical bits decomposition (noir-lang/noir#7168) fix: Keep `inc_rc` for array inputs during preprocessing (noir-lang/noir#7163) fix(docs): Update broken links to EC lib (noir-lang/noir#7141) feat: inline simple functions (noir-lang/noir#7160) feat(ssa): Expand feature set of the Brillig constraint check (noir-lang/noir#7060) fix(ssa): Resolve value before fetching from DFG in a couple cases (noir-lang/noir#7169) fix: `Function::is_no_predicates` always returned false for brillig f… (noir-lang/noir#7167) chore(refactor): Remove globals field on Ssa object and use only the shared globals graph (noir-lang/noir#7156) chore: let `Function::inlined` take a `should_inline_call` function (noir-lang/noir#7149) chore: add compile-time assertions on generic arguments of stdlib functions (noir-lang/noir#6981) fix: LSP hover over function with `&mut self` (noir-lang/noir#7155) feat(brillig): Set global memory size at program compile time (noir-lang/noir#7151) feat: LSP autocomplete module declaration (noir-lang/noir#7154) feat(ssa): Reuse constants from the globals graph when making constants in a function DFG (noir-lang/noir#7153) feat: LSP chain inlay hints (noir-lang/noir#7152) chore: turn on overflow checks in CI rust tests (noir-lang/noir#7145) fix(ssa): Use post order when mapping instructions in loop invariant pass (noir-lang/noir#7140) fix: preserve types when reading from calldata arrays (noir-lang/noir#7144) feat: Resolve enums & prepare type system (noir-lang/noir#7115) feat: `loop` must have at least one `break` (noir-lang/noir#7126) feat: parse globals in SSA parser (noir-lang/noir#7112) fix: allow calling trait impl method from struct if multiple impls exist (noir-lang/noir#7124) fix: avoid creating unnecessary memory blocks (noir-lang/noir#7114) chore: relax threshold for reporting regressions (noir-lang/noir#7130) fix: proper cleanup when breaking from comptime loop on error (noir-lang/noir#7125) fix: Prevent overlapping associated types impls (noir-lang/noir#7047) feat: unconstrained optimizations for BoundedVec (noir-lang/noir#7119) chore: mark libs good (noir-lang/noir#7123) chore: remove comments for time/memory benchmarks (noir-lang/noir#7121) fix: don't always use an exclusive lock in `nargo check` (noir-lang/noir#7120) feat(ssa): Pass to preprocess functions (noir-lang/noir#7072) chore: Formatting issues / minor errors in the docs (noir-lang/noir#7105) fix: defunctionalize pass on the caller runtime to apply (noir-lang/noir#7100) feat: Parser and formatter support for `enum`s (noir-lang/noir#7110) feat(brillig): SSA globals code gen (noir-lang/noir#7021) feat: `loop` keyword in runtime and comptime code (noir-lang/noir#7096) chore: Add benchmarking dashboard (noir-lang/noir#7068) feat(experimental): try to infer lambda argument types inside calls (noir-lang/noir#7088) feat(ssa): Add flag to DIE pass to be able to keep `store` instructions (noir-lang/noir#7106) chore: Cookbook Onboard integration (noir-lang/noir#7044) chore: lock to ubuntu 22.04 (noir-lang/noir#7098) fix: Remove unused brillig functions (noir-lang/noir#7102) chore(ssa): Use correct prefix when printing array values in global space (noir-lang/noir#7095)
- Loading branch information
Showing
44 changed files
with
1,289 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
a0704aa53250aed9c5460a60f5aaffa87772732f | ||
c44b62615f1c8ee657eedd82f2b80e2ec76c9078 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
noir/noir-repo/compiler/noirc_frontend/src/elaborator/enums.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use iter_extended::vecmap; | ||
use noirc_errors::Location; | ||
|
||
use crate::{ | ||
ast::{EnumVariant, FunctionKind, NoirEnumeration, UnresolvedType, Visibility}, | ||
hir_def::{ | ||
expr::{HirEnumConstructorExpression, HirExpression, HirIdent}, | ||
function::{FuncMeta, FunctionBody, HirFunction, Parameters}, | ||
stmt::HirPattern, | ||
}, | ||
node_interner::{DefinitionKind, FuncId, FunctionModifiers, TypeId}, | ||
token::Attributes, | ||
DataType, Shared, Type, | ||
}; | ||
|
||
use super::Elaborator; | ||
|
||
impl Elaborator<'_> { | ||
#[allow(clippy::too_many_arguments)] | ||
pub(super) fn define_enum_variant_function( | ||
&mut self, | ||
enum_: &NoirEnumeration, | ||
type_id: TypeId, | ||
variant: &EnumVariant, | ||
variant_arg_types: Vec<Type>, | ||
variant_index: usize, | ||
datatype: &Shared<DataType>, | ||
self_type: &Type, | ||
self_type_unresolved: UnresolvedType, | ||
) { | ||
let name_string = variant.name.to_string(); | ||
let datatype_ref = datatype.borrow(); | ||
let location = Location::new(variant.name.span(), self.file); | ||
|
||
let id = self.interner.push_empty_fn(); | ||
|
||
let modifiers = FunctionModifiers { | ||
name: name_string.clone(), | ||
visibility: enum_.visibility, | ||
attributes: Attributes { function: None, secondary: Vec::new() }, | ||
is_unconstrained: false, | ||
generic_count: datatype_ref.generics.len(), | ||
is_comptime: false, | ||
name_location: location, | ||
}; | ||
let definition_id = | ||
self.interner.push_function_definition(id, modifiers, type_id.module_id(), location); | ||
|
||
let hir_name = HirIdent::non_trait_method(definition_id, location); | ||
let parameters = self.make_enum_variant_parameters(variant_arg_types, location); | ||
self.push_enum_variant_function_body(id, datatype, variant_index, ¶meters, location); | ||
|
||
let function_type = | ||
datatype_ref.variant_function_type_with_forall(variant_index, datatype.clone()); | ||
self.interner.push_definition_type(definition_id, function_type.clone()); | ||
|
||
let meta = FuncMeta { | ||
name: hir_name, | ||
kind: FunctionKind::Normal, | ||
parameters, | ||
parameter_idents: Vec::new(), | ||
return_type: crate::ast::FunctionReturnType::Ty(self_type_unresolved), | ||
return_visibility: Visibility::Private, | ||
typ: function_type, | ||
direct_generics: datatype_ref.generics.clone(), | ||
all_generics: datatype_ref.generics.clone(), | ||
location, | ||
has_body: false, | ||
trait_constraints: Vec::new(), | ||
type_id: Some(type_id), | ||
trait_id: None, | ||
trait_impl: None, | ||
enum_variant_index: Some(variant_index), | ||
is_entry_point: false, | ||
has_inline_attribute: false, | ||
function_body: FunctionBody::Resolved, | ||
source_crate: self.crate_id, | ||
source_module: type_id.local_module_id(), | ||
source_file: self.file, | ||
self_type: None, | ||
}; | ||
|
||
self.interner.push_fn_meta(meta, id); | ||
self.interner.add_method(self_type, name_string, id, None); | ||
|
||
let name = variant.name.clone(); | ||
Self::get_module_mut(self.def_maps, type_id.module_id()) | ||
.declare_function(name, enum_.visibility, id) | ||
.ok(); | ||
} | ||
|
||
// Given: | ||
// ``` | ||
// enum FooEnum { Foo(u32, u8), ... } | ||
// | ||
// fn Foo(a: u32, b: u8) -> FooEnum {} | ||
// ``` | ||
// Create (pseudocode): | ||
// ``` | ||
// fn Foo(a: u32, b: u8) -> FooEnum { | ||
// // This can't actually be written directly in Noir | ||
// FooEnum { | ||
// tag: Foo_tag, | ||
// Foo: (a, b), | ||
// // fields from other variants are zeroed in monomorphization | ||
// } | ||
// } | ||
// ``` | ||
fn push_enum_variant_function_body( | ||
&mut self, | ||
id: FuncId, | ||
self_type: &Shared<DataType>, | ||
variant_index: usize, | ||
parameters: &Parameters, | ||
location: Location, | ||
) { | ||
// Each parameter of the enum variant function is used as a parameter of the enum | ||
// constructor expression | ||
let arguments = vecmap(¶meters.0, |(pattern, typ, _)| match pattern { | ||
HirPattern::Identifier(ident) => { | ||
let id = self.interner.push_expr(HirExpression::Ident(ident.clone(), None)); | ||
self.interner.push_expr_type(id, typ.clone()); | ||
self.interner.push_expr_location(id, location.span, location.file); | ||
id | ||
} | ||
_ => unreachable!(), | ||
}); | ||
|
||
let enum_generics = self_type.borrow().generic_types(); | ||
let construct_variant = HirExpression::EnumConstructor(HirEnumConstructorExpression { | ||
r#type: self_type.clone(), | ||
enum_generics: enum_generics.clone(), | ||
arguments, | ||
variant_index, | ||
}); | ||
let body = self.interner.push_expr(construct_variant); | ||
self.interner.update_fn(id, HirFunction::unchecked_from_expr(body)); | ||
|
||
let typ = Type::DataType(self_type.clone(), enum_generics); | ||
self.interner.push_expr_type(body, typ); | ||
self.interner.push_expr_location(body, location.span, location.file); | ||
} | ||
|
||
fn make_enum_variant_parameters( | ||
&mut self, | ||
parameter_types: Vec<Type>, | ||
location: Location, | ||
) -> Parameters { | ||
Parameters(vecmap(parameter_types.into_iter().enumerate(), |(i, parameter_type)| { | ||
let name = format!("${i}"); | ||
let parameter = DefinitionKind::Local(None); | ||
let id = self.interner.push_definition(name, false, false, parameter, location); | ||
let pattern = HirPattern::Identifier(HirIdent::non_trait_method(id, location)); | ||
(pattern, parameter_type, Visibility::Private) | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.