Skip to content

Commit

Permalink
Merge branch 'master' into jf/trait-attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jul 12, 2024
2 parents 6e03c81 + 299703c commit fcd9df2
Show file tree
Hide file tree
Showing 42 changed files with 1,940 additions and 713 deletions.
19 changes: 13 additions & 6 deletions compiler/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ pub fn create_program(
let recursive = program.recursive;
let ArtifactsAndWarnings((generated_acirs, generated_brillig, error_types), ssa_level_warnings) =
optimize_into_acir(program, options)?;
assert_eq!(
generated_acirs.len(),
func_sigs.len(),
"The generated ACIRs should match the supplied function signatures"
);

if options.force_brillig_output {
assert_eq!(
generated_acirs.len(),
1,
"Only the main ACIR is expected when forcing Brillig output"
);
} else {
assert_eq!(
generated_acirs.len(),
func_sigs.len(),
"The generated ACIRs should match the supplied function signatures"
);
}
let mut program_artifact = SsaProgramArtifact::new(generated_brillig, error_types);

// Add warnings collected at the Ssa stage
Expand Down
11 changes: 8 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use acvm::{acir::AcirField, FieldElement};
use iter_extended::vecmap;
use noirc_errors::Location;
use noirc_frontend::ast::{BinaryOpKind, Signedness};
use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters};
use noirc_frontend::monomorphization::ast::{self, InlineType, LocalId, Parameters};
use noirc_frontend::monomorphization::ast::{FuncId, Program};

use crate::errors::RuntimeError;
Expand Down Expand Up @@ -121,9 +121,14 @@ impl<'a> FunctionContext<'a> {
///
/// Note that the previous function cannot be resumed after calling this. Developers should
/// avoid calling new_function until the previous function is completely finished with ssa-gen.
pub(super) fn new_function(&mut self, id: IrFunctionId, func: &ast::Function) {
pub(super) fn new_function(
&mut self,
id: IrFunctionId,
func: &ast::Function,
force_brillig_runtime: bool,
) {
self.definitions.clear();
if func.unconstrained {
if func.unconstrained || (force_brillig_runtime && func.inline_type != InlineType::Inline) {
self.builder.new_brillig_function(func.name.clone(), id);
} else {
self.builder.new_function(func.name.clone(), id, func.inline_type);
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub(crate) fn generate_ssa(
// to generate SSA for each function used within the program.
while let Some((src_function_id, dest_id)) = context.pop_next_function_in_queue() {
let function = &context.program[src_function_id];
function_context.new_function(dest_id, function);
function_context.new_function(dest_id, function, force_brillig_runtime);
function_context.codegen_function_body(&function.body)?;
}

Expand Down
9 changes: 7 additions & 2 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl<'context> Elaborator<'context> {
let (mut object, mut object_type) = self.elaborate_expression(method_call.object);
object_type = object_type.follow_bindings();

let method_name_span = method_call.method_name.span();
let method_name = method_call.method_name.0.contents.as_str();
match self.lookup_method(&object_type, method_name, span) {
Some(method_ref) => {
Expand Down Expand Up @@ -385,6 +386,9 @@ impl<'context> Elaborator<'context> {

self.interner.push_expr_type(function_id, func_type.clone());

self.interner
.add_function_reference(func_id, Location::new(method_name_span, self.file));

// Type check the new call now that it has been changed from a method call
// to a function call. This way we avoid duplicating code.
let typ = self.type_check_call(&function_call, func_type, function_args, span);
Expand All @@ -399,7 +403,8 @@ impl<'context> Elaborator<'context> {
constructor: ConstructorExpression,
) -> (HirExpression, Type) {
let span = constructor.type_name.span();
let is_self_type = constructor.type_name.last_segment().is_self_type_name();
let last_segment = constructor.type_name.last_segment();
let is_self_type = last_segment.is_self_type_name();

let (r#type, struct_generics) = if let Some(struct_id) = constructor.struct_type {
let typ = self.interner.get_struct(struct_id);
Expand Down Expand Up @@ -430,7 +435,7 @@ impl<'context> Elaborator<'context> {
});

let struct_id = struct_type.borrow().id;
let reference_location = Location::new(span, self.file);
let reference_location = Location::new(last_segment.span(), self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

(expr, Type::Struct(struct_type, generics))
Expand Down
12 changes: 10 additions & 2 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,20 @@ impl<'context> Elaborator<'context> {
let statements = std::mem::take(&mut func.def.body.statements);
let body = BlockExpression { statements };

let struct_id = if let Some(Type::Struct(struct_type, _)) = &self.self_type {
Some(struct_type.borrow().id)
} else {
None
};

let meta = FuncMeta {
name: name_ident,
kind: func.kind,
location,
typ,
direct_generics,
all_generics: self.generics.clone(),
struct_id,
trait_impl: self.current_trait_impl,
parameters: parameters.into(),
parameter_idents,
Expand Down Expand Up @@ -1236,7 +1243,7 @@ impl<'context> Elaborator<'context> {

for field_index in 0..fields_len {
self.interner
.add_definition_location(ReferenceId::StructMember(type_id, field_index));
.add_definition_location(ReferenceId::StructMember(type_id, field_index), None);
}

let item = Value::StructDefinition(type_id);
Expand Down Expand Up @@ -1431,7 +1438,8 @@ impl<'context> Elaborator<'context> {
self.elaborate_comptime_global(global_id);
}

self.interner.add_definition_location(ReferenceId::Global(global_id));
self.interner
.add_definition_location(ReferenceId::Global(global_id), Some(self.module_id()));

self.local_module = old_module;
self.file = old_file;
Expand Down
16 changes: 14 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::hir::resolution::path_resolver::{PathResolver, StandardPathResolver};
use crate::hir::resolution::resolver::SELF_TYPE_NAME;
use crate::hir::scope::{Scope as GenericScope, ScopeTree as GenericScopeTree};
use crate::macros_api::Ident;
use crate::node_interner::ReferenceId;
use crate::{
hir::{
def_map::{ModuleDefId, TryFromModuleDefId},
Expand Down Expand Up @@ -48,17 +47,30 @@ impl<'context> Elaborator<'context> {
let path_resolution;

if self.interner.track_references {
let mut references: Vec<ReferenceId> = Vec::new();
let last_segment = path.last_segment();
let location = Location::new(last_segment.span(), self.file);
let is_self_type_name = last_segment.is_self_type_name();

let mut references: Vec<_> = Vec::new();
path_resolution =
resolver.resolve(self.def_maps, path.clone(), &mut Some(&mut references))?;

for (referenced, ident) in references.iter().zip(path.segments) {
let Some(referenced) = referenced else {
continue;
};
self.interner.add_reference(
*referenced,
Location::new(ident.span(), self.file),
ident.is_self_type_name(),
);
}

self.interner.add_module_def_id_reference(
path_resolution.module_def_id,
location,
is_self_type_name,
);
} else {
path_resolution = resolver.resolve(self.def_maps, path, &mut None)?;
}
Expand Down
18 changes: 11 additions & 7 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ impl<'context> Elaborator<'context> {
use crate::ast::UnresolvedTypeData::*;

let span = typ.span;
let (is_self_type_name, is_synthetic) = if let Named(ref named_path, _, synthetic) = typ.typ
{
(named_path.last_segment().is_self_type_name(), synthetic)
} else {
(false, false)
};
let (named_path_span, is_self_type_name, is_synthetic) =
if let Named(ref named_path, _, synthetic) = typ.typ {
(
Some(named_path.last_segment().span()),
named_path.last_segment().is_self_type_name(),
synthetic,
)
} else {
(None, false, false)
};

let resolved_type = match typ.typ {
FieldElement => Type::FieldElement,
Expand Down Expand Up @@ -153,7 +157,7 @@ impl<'context> Elaborator<'context> {
};

if let Some(unresolved_span) = typ.span {
let location = Location::new(unresolved_span, self.file);
let location = Location::new(named_path_span.unwrap_or(unresolved_span), self.file);

match resolved_type {
Type::Struct(ref struct_type, _) => {
Expand Down
27 changes: 5 additions & 22 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl DefCollector {
for collected_import in std::mem::take(&mut def_collector.imports) {
let module_id = collected_import.module_id;
let resolved_import = if context.def_interner.track_references {
let mut references: Vec<ReferenceId> = Vec::new();
let mut references: Vec<Option<ReferenceId>> = Vec::new();
let resolved_import = resolve_import(
crate_id,
&collected_import,
Expand All @@ -359,6 +359,9 @@ impl DefCollector {
let file_id = current_def_map.file_id(module_id);

for (referenced, ident) in references.iter().zip(&collected_import.path.segments) {
let Some(referenced) = referenced else {
continue;
};
context.def_interner.add_reference(
*referenced,
Location::new(ident.span(), file_id),
Expand Down Expand Up @@ -552,27 +555,7 @@ fn add_import_reference(
}

let location = Location::new(name.span(), file_id);

match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => {
interner.add_module_reference(module_id, location);
}
crate::macros_api::ModuleDefId::FunctionId(func_id) => {
interner.add_function_reference(func_id, location);
}
crate::macros_api::ModuleDefId::TypeId(struct_id) => {
interner.add_struct_reference(struct_id, location, false);
}
crate::macros_api::ModuleDefId::TraitId(trait_id) => {
interner.add_trait_reference(trait_id, location, false);
}
crate::macros_api::ModuleDefId::TypeAliasId(type_alias_id) => {
interner.add_alias_reference(type_alias_id, location);
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => {
interner.add_global_reference(global_id, location);
}
};
interner.add_module_def_id_reference(def_id, location, false);
}

fn inject_prelude(
Expand Down
29 changes: 23 additions & 6 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::ast::{
TypeImpl,
};
use crate::macros_api::NodeInterner;
use crate::node_interner::ReferenceId;
use crate::node_interner::{ModuleAttributes, ReferenceId};
use crate::{
graph::CrateId,
hir::def_collector::dc_crate::{UnresolvedStruct, UnresolvedTrait},
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn collect_defs(

errors.extend(collector.collect_structs(context, ast.types, crate_id));

errors.extend(collector.collect_type_aliases(context, ast.type_aliases));
errors.extend(collector.collect_type_aliases(context, ast.type_aliases, crate_id));

errors.extend(collector.collect_functions(context, ast.functions, crate_id));

Expand Down Expand Up @@ -318,7 +318,10 @@ impl<'a> ModCollector<'a> {
// And store the TypeId -> StructType mapping somewhere it is reachable
self.def_collector.items.types.insert(id, unresolved);

context.def_interner.add_definition_location(ReferenceId::Struct(id));
context.def_interner.add_definition_location(
ReferenceId::Struct(id),
Some(ModuleId { krate, local_id: self.module_id }),
);
}
definition_errors
}
Expand All @@ -329,6 +332,7 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
type_aliases: Vec<NoirTypeAlias>,
krate: CrateId,
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
for type_alias in type_aliases {
Expand Down Expand Up @@ -365,7 +369,10 @@ impl<'a> ModCollector<'a> {

self.def_collector.items.type_aliases.insert(type_alias_id, unresolved);

context.def_interner.add_definition_location(ReferenceId::Alias(type_alias_id));
context.def_interner.add_definition_location(
ReferenceId::Alias(type_alias_id),
Some(ModuleId { krate, local_id: self.module_id }),
);
}
errors
}
Expand Down Expand Up @@ -532,7 +539,10 @@ impl<'a> ModCollector<'a> {
};
context.def_interner.push_empty_trait(trait_id, &unresolved, resolved_generics);

context.def_interner.add_definition_location(ReferenceId::Trait(trait_id));
context.def_interner.add_definition_location(
ReferenceId::Trait(trait_id),
Some(ModuleId { krate, local_id: self.module_id }),
);

self.def_collector.items.traits.insert(trait_id, unresolved);
}
Expand Down Expand Up @@ -720,7 +730,14 @@ impl<'a> ModCollector<'a> {
return Err(err);
}

context.def_interner.add_module_location(mod_id, mod_location);
context.def_interner.add_module_attributes(
mod_id,
ModuleAttributes {
name: mod_name.0.contents.clone(),
location: mod_location,
parent: self.module_id,
},
);
}

Ok(mod_id)
Expand Down
Loading

0 comments on commit fcd9df2

Please sign in to comment.