Skip to content

Commit

Permalink
Easier way to track references
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 10, 2024
1 parent 5e54385 commit 2d4d013
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 81 deletions.
18 changes: 9 additions & 9 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
HirLiteral, HirStatement, Ident, IndexExpression, Literal, MemberAccessExpression,
MethodCallExpression, PrefixExpression,
},
node_interner::{DefinitionKind, ExprId, FuncId, ReferenceId},
node_interner::{DefinitionKind, ExprId, FuncId},
token::Tokens,
QuotedType, Shared, StructType, Type,
};
Expand Down Expand Up @@ -418,9 +418,9 @@ impl<'context> Elaborator<'context> {
struct_generics,
});

let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(Location::new(span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let struct_id = struct_type.borrow().id;
let reference_location = Location::new(span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

(expr, Type::Struct(struct_type, generics))
}
Expand Down Expand Up @@ -474,11 +474,11 @@ impl<'context> Elaborator<'context> {
}

if let Some(expected_index) = expected_index {
let struct_id = struct_type.borrow().id;
let referenced = ReferenceId::StructMember(struct_id, expected_index);
let reference =
ReferenceId::Reference(Location::new(field_name.span(), self.file), false);
self.interner.add_reference(referenced, reference);
self.interner.add_struct_member_reference(
struct_type.borrow().id,
expected_index,
Location::new(field_name.span(), self.file),
);
}

ret.push((field_name, resolved));
Expand Down
6 changes: 2 additions & 4 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,13 +1457,11 @@ impl<'context> Elaborator<'context> {

if let Some(trait_id) = trait_id {
let trait_name = trait_impl.trait_path.last_segment();

let referenced = ReferenceId::Trait(trait_id);
let reference = ReferenceId::Reference(
self.interner.add_trait_reference(
trait_id,
Location::new(trait_name.span(), trait_impl.file_id),
trait_name.is_self_type_name(),
);
self.interner.add_reference(referenced, reference);
}
}
}
Expand Down
31 changes: 9 additions & 22 deletions compiler/noirc_frontend/src/elaborator/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use crate::{
stmt::HirPattern,
},
macros_api::{HirExpression, Ident, Path, Pattern},
node_interner::{
DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, ReferenceId, TraitImplKind,
},
node_interner::{DefinitionId, DefinitionKind, ExprId, FuncId, GlobalId, TraitImplKind},
Shared, StructType, Type, TypeBindings,
};

Expand Down Expand Up @@ -204,14 +202,12 @@ impl<'context> Elaborator<'context> {

let struct_id = struct_type.borrow().id;

let referenced = ReferenceId::Struct(struct_id);
let reference = ReferenceId::Reference(Location::new(name_span, self.file), is_self_type);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(name_span, self.file);
self.interner.add_struct_reference(struct_id, reference_location, is_self_type);

for (field_index, field) in fields.iter().enumerate() {
let referenced = ReferenceId::StructMember(struct_id, field_index);
let reference = ReferenceId::Reference(Location::new(field.0.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(field.0.span(), self.file);
self.interner.add_struct_member_reference(struct_id, field_index, reference_location);
}

HirPattern::Struct(expected_type, fields, location)
Expand Down Expand Up @@ -487,7 +483,6 @@ impl<'context> Elaborator<'context> {
// This lookup allows support of such statements: let x = foo::bar::SOME_GLOBAL + 10;
// If the expression is a singular indent, we search the resolver's current scope as normal.
let span = path.span();
let is_self_type_name = path.last_segment().is_self_type_name();
let (hir_ident, var_scope_index) = self.get_ident_from_path(path);

if hir_ident.id != DefinitionId::dummy_id() {
Expand All @@ -497,10 +492,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_function_dependency(current_item, func_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let function = ReferenceId::Function(func_id);
self.interner.add_reference(function, variable);
self.interner.add_function_reference(func_id, hir_ident.location);
}
DefinitionKind::Global(global_id) => {
if let Some(global) = self.unresolved_globals.remove(&global_id) {
Expand All @@ -510,10 +502,7 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, global_id);
}

let variable =
ReferenceId::Reference(hir_ident.location, is_self_type_name);
let global = ReferenceId::Global(global_id);
self.interner.add_reference(global, variable);
self.interner.add_global_reference(global_id, hir_ident.location);
}
DefinitionKind::GenericType(_) => {
// Initialize numeric generics to a polymorphic integer type in case
Expand All @@ -529,10 +518,8 @@ impl<'context> Elaborator<'context> {
// only local variables can be captured by closures.
self.resolve_local_variable(hir_ident.clone(), var_scope_index);

let referenced = ReferenceId::Local(hir_ident.id);
let reference =
ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(hir_ident.id, reference_location);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl<'context> Elaborator<'context> {
resolver.resolve(self.def_maps, path.clone(), &mut Some(&mut references))?;

for (referenced, ident) in references.iter().zip(path.segments) {
let reference = ReferenceId::Reference(
self.interner.add_reference(
*referenced,
Location::new(ident.span(), self.file),
ident.is_self_type_name(),
);
self.interner.add_reference(*referenced, reference);
}
} else {
path_resolution = resolver.resolve(self.def_maps, path, &mut None)?;
Expand Down
12 changes: 5 additions & 7 deletions compiler/noirc_frontend/src/elaborator/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
macros_api::{
ForLoopStatement, ForRange, HirStatement, LetStatement, Path, Statement, StatementKind,
},
node_interner::{DefinitionId, DefinitionKind, GlobalId, ReferenceId, StmtId},
node_interner::{DefinitionId, DefinitionKind, GlobalId, StmtId},
Type,
};

Expand Down Expand Up @@ -256,9 +256,8 @@ impl<'context> Elaborator<'context> {
typ.follow_bindings()
};

let referenced = ReferenceId::Local(ident.id);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_local_reference(ident.id, reference_location);

(HirLValue::Ident(ident.clone(), typ.clone()), typ, mutable)
}
Expand Down Expand Up @@ -381,9 +380,8 @@ impl<'context> Elaborator<'context> {
Type::Struct(s, args) => {
let s = s.borrow();
if let Some((field, index)) = s.get_field(field_name, args) {
let referenced = ReferenceId::StructMember(s.id, index);
let reference = ReferenceId::Reference(Location::new(span, self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(span, self.file);
self.interner.add_struct_member_reference(s.id, index, reference_location);

return Some((field, index));
}
Expand Down
30 changes: 10 additions & 20 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ use crate::{
UnaryOp, UnresolvedType, UnresolvedTypeData,
},
node_interner::{
DefinitionKind, DependencyId, ExprId, GlobalId, ReferenceId, TraitId, TraitImplKind,
TraitMethodId,
DefinitionKind, DependencyId, ExprId, GlobalId, TraitId, TraitImplKind, TraitMethodId,
},
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeVariable, TypeVariableKind,
};
Expand Down Expand Up @@ -154,30 +153,23 @@ impl<'context> Elaborator<'context> {
};

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

match resolved_type {
Type::Struct(ref struct_type, _) => {
// Record the location of the type reference
self.interner.push_type_ref_location(
resolved_type.clone(),
Location::new(unresolved_span, self.file),
);
self.interner.push_type_ref_location(resolved_type.clone(), location);

if !is_synthetic {
let referenced = ReferenceId::Struct(struct_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
self.interner.add_struct_reference(
struct_type.borrow().id,
location,
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
}
}
Type::Alias(ref alias_type, _) => {
let referenced = ReferenceId::Alias(alias_type.borrow().id);
let reference = ReferenceId::Reference(
Location::new(unresolved_span, self.file),
is_self_type_name,
);
self.interner.add_reference(referenced, reference);
self.interner.add_alias_reference(alias_type.borrow().id, location);
}
_ => (),
}
Expand Down Expand Up @@ -369,10 +361,8 @@ impl<'context> Elaborator<'context> {
self.interner.add_global_dependency(current_item, id);
}

let referenced = ReferenceId::Global(id);
let reference =
ReferenceId::Reference(Location::new(path.span(), self.file), false);
self.interner.add_reference(referenced, reference);
let reference_location = Location::new(path.span(), self.file);
self.interner.add_global_reference(id, reference_location);

Some(Type::Constant(self.eval_global_as_array_length(id, path)))
}
Expand Down
36 changes: 24 additions & 12 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ impl DefCollector {
let file_id = current_def_map.file_id(module_id);

for (referenced, ident) in references.iter().zip(&collected_import.path.segments) {
let reference =
ReferenceId::Reference(Location::new(ident.span(), file_id), false);
context.def_interner.add_reference(*referenced, reference);
context.def_interner.add_reference(
*referenced,
Location::new(ident.span(), file_id),
false,
);
}

resolved_import
Expand Down Expand Up @@ -511,18 +513,28 @@ fn add_import_reference(
return;
}

let referenced = match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => ReferenceId::Module(module_id),
crate::macros_api::ModuleDefId::FunctionId(func_id) => ReferenceId::Function(func_id),
crate::macros_api::ModuleDefId::TypeId(struct_id) => ReferenceId::Struct(struct_id),
crate::macros_api::ModuleDefId::TraitId(trait_id) => ReferenceId::Trait(trait_id),
let location = Location::new(name.span(), file_id);

match def_id {
crate::macros_api::ModuleDefId::ModuleId(module_id) => {
interner.add_module_location(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) => {
ReferenceId::Alias(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)
}
crate::macros_api::ModuleDefId::GlobalId(global_id) => ReferenceId::Global(global_id),
};
let reference = ReferenceId::Reference(Location::new(name.span(), file_id), false);
interner.add_reference(referenced, reference);
}

fn inject_prelude(
Expand Down
4 changes: 1 addition & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,7 @@ impl<'a> ModCollector<'a> {
) {
Ok(child_mod_id) => {
// Track that the "foo" in `mod foo;` points to the module "foo"
let referenced = ReferenceId::Module(child_mod_id);
let reference = ReferenceId::Reference(location, false);
context.def_interner.add_reference(referenced, reference);
context.def_interner.add_module_reference(child_mod_id, location);

errors.extend(collect_defs(
self.def_collector,
Expand Down
62 changes: 60 additions & 2 deletions compiler/noirc_frontend/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use noirc_errors::Location;
use rangemap::RangeMap;
use rustc_hash::FxHashMap;

use crate::{macros_api::NodeInterner, node_interner::ReferenceId};
use crate::{
hir::def_map::ModuleId,
macros_api::{NodeInterner, StructId},
node_interner::{DefinitionId, FuncId, GlobalId, ReferenceId, TraitId, TypeAliasId},
};
use petgraph::prelude::NodeIndex as PetGraphIndex;

#[derive(Debug, Default)]
Expand Down Expand Up @@ -58,11 +62,65 @@ impl NodeInterner {
}
}

pub(crate) fn add_reference(&mut self, referenced: ReferenceId, reference: ReferenceId) {
pub(crate) fn add_module_reference(&mut self, id: ModuleId, location: Location) {
self.add_reference(ReferenceId::Module(id), location, false);
}

pub(crate) fn add_struct_reference(
&mut self,
id: StructId,
location: Location,
is_self_type: bool,
) {
self.add_reference(ReferenceId::Struct(id), location, is_self_type);
}

pub(crate) fn add_struct_member_reference(
&mut self,
id: StructId,
member_index: usize,
location: Location,
) {
self.add_reference(ReferenceId::StructMember(id, member_index), location, false);
}

pub(crate) fn add_trait_reference(
&mut self,
id: TraitId,
location: Location,
is_self_type: bool,
) {
self.add_reference(ReferenceId::Trait(id), location, is_self_type);
}

pub(crate) fn add_alias_reference(&mut self, id: TypeAliasId, location: Location) {
self.add_reference(ReferenceId::Alias(id), location, false);
}

pub(crate) fn add_function_reference(&mut self, id: FuncId, location: Location) {
self.add_reference(ReferenceId::Function(id), location, false);
}

pub(crate) fn add_global_reference(&mut self, id: GlobalId, location: Location) {
self.add_reference(ReferenceId::Global(id), location, false);
}

pub(crate) fn add_local_reference(&mut self, id: DefinitionId, location: Location) {
self.add_reference(ReferenceId::Local(id), location, false);
}

pub(crate) fn add_reference(
&mut self,
referenced: ReferenceId,
location: Location,
is_self_type: bool,
) {
if !self.track_references {
return;
}

let reference = ReferenceId::Reference(location, is_self_type);

let referenced_index = self.get_or_insert_reference(referenced);
let reference_location = self.reference_location(reference);
let reference_index = self.reference_graph.add_node(reference);
Expand Down

0 comments on commit 2d4d013

Please sign in to comment.