Skip to content

Commit

Permalink
GlobalResolver now contains ResolvedGlobals itself
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 20, 2024
1 parent e4f047a commit 2d71d73
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/flattening.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Deref, iter::zip, cell::RefCell};
use std::{ops::Deref, iter::zip};

use crate::{
ast::{Span, Module, Expression, SpanExpression, LocalOrGlobal, Operator, AssignableExpression, SpanAssignableExpression, Statement, CodeBlock, IdentifierType, TypeExpression, DeclIDMarker, DeclID, SpanTypeExpression, InterfacePorts},
Expand Down Expand Up @@ -169,13 +169,13 @@ impl Instantiation {
}
}

struct FlatteningContext<'inst, 'l, 'm, 'resolved> {
struct FlatteningContext<'inst, 'l, 'm> {
decl_to_flat_map : FlatAlloc<Option<FlatID>, DeclIDMarker>,
instantiations : &'inst mut FlatAlloc<Instantiation, FlatIDMarker>,
errors : ErrorCollector,
is_remote_declaration : bool,

linker : GlobalResolver<'l, 'resolved>,
linker : GlobalResolver<'l>,
pub type_list_for_naming : &'l ArenaAllocator<NamedType, TypeUUIDMarker>,
module : &'m Module,
}
Expand All @@ -189,7 +189,7 @@ fn must_be_compiletime(wire : &WireInstance, context : &str, errors : &ErrorColl
must_be_compiletime_with_info(wire, context, errors, || Vec::new());
}

impl<'inst, 'l, 'm, 'resolved> FlatteningContext<'inst, 'l, 'm, 'resolved> {
impl<'inst, 'l, 'm> FlatteningContext<'inst, 'l, 'm> {
fn map_to_type(&mut self, type_expr : &SpanTypeExpression) -> Type {
match &type_expr.0 {
TypeExpression::Named => {
Expand Down Expand Up @@ -281,6 +281,8 @@ impl<'inst, 'l, 'm, 'resolved> FlatteningContext<'inst, 'l, 'm, 'resolved> {

let interface_ports = nested_context.initialize_interface::<true>();

self.linker.reabsorb_sublinker(nested_context.linker);

self.instantiations.alloc(Instantiation::SubModule(SubModuleInstance{
name,
module_uuid,
Expand Down Expand Up @@ -758,13 +760,12 @@ impl FlattenedModule {
*/
pub fn initialize(linker : &Linker, module : &Module) -> FlattenedModule {
let mut instantiations = FlatAlloc::new();
let resolved_globals : RefCell<ResolvedGlobals> = RefCell::new(ResolvedGlobals::new());
let mut context = FlatteningContext{
decl_to_flat_map: module.declarations.iter().map(|_| None).collect(),
instantiations: &mut instantiations,
errors: ErrorCollector::new(module.link_info.file),
is_remote_declaration : false,
linker : GlobalResolver::new(linker, module.link_info.file, &resolved_globals),
linker : GlobalResolver::new(linker, module.link_info.file),
type_list_for_naming : &linker.types,
module,
};
Expand All @@ -777,9 +778,9 @@ impl FlattenedModule {

FlattenedModule {
errors : context.errors,
resolved_globals : context.linker.extract_resolved_globals(),
instantiations : instantiations,
interface_ports,
resolved_globals : resolved_globals.into_inner()
}
}
}
36 changes: 28 additions & 8 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,46 @@ impl ResolvedGlobals {
}
}

pub struct GlobalResolver<'linker, 'resolved_list> {
pub struct GlobalResolver<'linker> {
linker : &'linker Linker,
file : &'linker FileData,
resolved_globals : &'resolved_list RefCell<ResolvedGlobals>
resolved_globals : RefCell<Option<ResolvedGlobals>>
}

impl<'linker, 'resolved_list> GlobalResolver<'linker, 'resolved_list> {
pub fn new(linker : &'linker Linker, file_id : FileUUID, resolved_globals : &'resolved_list RefCell<ResolvedGlobals>) -> GlobalResolver<'linker, 'resolved_list> {
impl<'linker> GlobalResolver<'linker> {
pub fn new(linker : &'linker Linker, file_id : FileUUID) -> GlobalResolver<'linker> {
GlobalResolver{
linker,
file : &linker.files[file_id],
resolved_globals
resolved_globals : RefCell::new(Some(ResolvedGlobals::new()))
}
}

pub fn new_sublinker(&self, file_id : FileUUID) -> GlobalResolver<'linker, 'resolved_list> {
pub fn extract_resolved_globals(&self) -> ResolvedGlobals {
let sub_resolved = self.resolved_globals.replace(None);
sub_resolved.unwrap()
}

pub fn new_sublinker(&self, file_id : FileUUID) -> GlobalResolver<'linker> {
let this_resolved = self.extract_resolved_globals();
GlobalResolver{
linker : self.linker,
file : &self.linker.files[file_id],
resolved_globals : self.resolved_globals
resolved_globals : RefCell::new(Some(this_resolved))
}
}

pub fn reabsorb_sublinker(&self, sub : Self) {
let sub_resolved = sub.extract_resolved_globals();
let old_should_be_none = self.resolved_globals.replace(Some(sub_resolved));
assert!(old_should_be_none.is_none());
}

pub fn resolve_global(&self, name_span : Span, errors : &ErrorCollector) -> Option<NameElem> {
let name = self.file.get_token_text(name_span.assert_is_single_token());

let mut resolved_globals = self.resolved_globals.borrow_mut();
let mut resolved_globals_borrow = self.resolved_globals.borrow_mut();
let resolved_globals = resolved_globals_borrow.as_mut().unwrap();
match self.linker.global_namespace.get(name) {
Some(NamespaceElement::Global(found)) => {
resolved_globals.referenced_globals.push(*found);
Expand Down Expand Up @@ -500,3 +513,10 @@ impl<'linker, 'resolved_list> GlobalResolver<'linker, 'resolved_list> {
&self.linker.types[index]
}
}

impl<'linker> Drop for GlobalResolver<'linker> {
fn drop(&mut self) {
// resolved_globals must have been consumed
assert!(self.resolved_globals.get_mut().is_none());
}
}

0 comments on commit 2d71d73

Please sign in to comment.