Skip to content

Commit

Permalink
Better registries (#24)
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
DasLixou authored May 12, 2024
1 parent 97c68e3 commit 10570c0
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 411 deletions.
2 changes: 1 addition & 1 deletion ohdlc/src/ir/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod import_bucket;
pub mod modules;
pub mod name_lookup;
pub mod registry;
pub mod registries;

pub mod stages;
5 changes: 3 additions & 2 deletions ohdlc/src/ir/name_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{

use super::{
import_bucket::{ImportId, LookupStrategy},
registry::TypeId,
registries::{EntityId, TypeId},
};

simple_key!(
Expand Down Expand Up @@ -107,6 +107,7 @@ pub enum Resolvable {

#[derive(Debug, Clone, Copy)]
pub enum Resolved {
Type(TypeId),
Module(ModuleId),
Type(TypeId),
Entity(EntityId),
}
23 changes: 23 additions & 0 deletions ohdlc/src/ir/registries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use surotto::{simple::SimpleSurotto, simple_key};

use super::{
modules::{Module, ModuleId},
stages::{
refine::registries::{Entity, Type},
rough::registries::{RoughEntity, RoughType},
},
};

pub type ModuleRegistry = SimpleSurotto<ModuleId, Module>;
pub type RoughTypeRegistry<'ast> = SimpleSurotto<TypeId, RoughType<'ast>>;
pub type RoughEntityRegistry<'ast> = SimpleSurotto<EntityId, RoughEntity<'ast>>;
pub type RefinedTypeRegistry<'ir> = SimpleSurotto<TypeId, Type<'ir>>;
pub type RefinedEntityRegistry<'ir> = SimpleSurotto<EntityId, Entity<'ir>>;

simple_key!(
pub struct TypeId;
);

simple_key!(
pub struct EntityId;
);
25 changes: 0 additions & 25 deletions ohdlc/src/ir/registry.rs

This file was deleted.

16 changes: 9 additions & 7 deletions ohdlc/src/ir/stages/flatten_lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ use crate::{
name_lookup::{
LookupScope, PostFlattenNameLookup, PreFlattenNameLookup, Resolvable, Resolved,
},
registry::Registry,
registries::ModuleRegistry,
},
message::Message,
MESSAGES,
};

use super::rough::types::RoughType;

pub struct FlattenLookupStage<'ir, 'b, 'ast> {
pub registry: &'b Registry<RoughType<'ast>>,
pub struct FlattenLookupStage<'ir, 'b> {
pub module_reg: &'b ModuleRegistry,
pub name_lookup: PreFlattenNameLookup,
pub import_bucket: ImportBucket<'ir>,
pub resolvables: Vec<ImportId>,
}

impl<'ir> FlattenLookupStage<'ir, '_, '_> {
impl<'ir> FlattenLookupStage<'ir, '_> {
pub fn lower(mut self) -> Option<PostFlattenNameLookup> {
self.build_start_dependencies();

Expand Down Expand Up @@ -56,8 +54,12 @@ impl<'ir> FlattenLookupStage<'ir, '_, '_> {
Resolved::Type(_) => {
MESSAGES.report(Message::use_continues_after_type(segment.1));
}
Resolved::Entity(_) => {
// TODO: better error, this is actually an entity
MESSAGES.report(Message::use_continues_after_type(segment.1));
}
Resolved::Module(m) => {
let module = &self.registry.modules[m];
let module = &self.module_reg[m];
let sub_path = &import.path[1..];
import.scope = module.scope;
import.strategy = LookupStrategy::Direct;
Expand Down
2 changes: 1 addition & 1 deletion ohdlc/src/ir/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod rough;

pub mod flatten_lookup;

pub mod refine_types;
pub mod refine;
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,77 @@ use crate::{
ir::{
import_bucket::LookupStrategy,
name_lookup::{PostFlattenNameLookup, Resolved, ScopeId},
registry::{ModuleRegistry, TypeId, TypeRegistry},
registries::{
EntityId, ModuleRegistry, RefinedEntityRegistry, RefinedTypeRegistry,
RoughEntityRegistry, RoughTypeRegistry, TypeId,
},
},
message::Message,
MESSAGES,
};

use self::types::{Entity, Enum, Field, Port, Record, RefinedType, Variant};
use self::registries::{Entity, Enum, Field, Port, Record, Type, Variant};

use super::rough::types::{RoughType, RoughTypeItem};
use super::rough::registries::RoughType;

pub mod types;
pub mod registries;

pub struct RefineTypesStage<'ir, 'b> {
pub struct RefineStage<'ir, 'b> {
pub arena: &'ir Bump,
pub name_lookup: &'b PostFlattenNameLookup,
pub module_registry: &'b ModuleRegistry,
}

impl<'ir, 'ast> RefineTypesStage<'ir, '_> {
pub fn lower(self, registry: TypeRegistry<RoughType<'ast>>) -> TypeRegistry<RefinedType<'ir>> {
impl<'ir, 'ast> RefineStage<'ir, '_> {
pub fn refine_types(&self, registry: RoughTypeRegistry<'ast>) -> RefinedTypeRegistry<'ir> {
registry.map(|id, rough| self.refine_type(id, rough))
}

fn refine_type(&self, id: TypeId, rough: RoughType<'ast>) -> RefinedType<'ir> {
match rough.1 {
RoughTypeItem::Entity(e) => self.refine_entity(id, rough.0, e),
RoughTypeItem::Record(r) => self.refine_record(id, rough.0, r),
RoughTypeItem::Enum(e) => self.refine_enum(id, e),
pub fn refine_entities(
&self,
registry: RoughEntityRegistry<'ast>,
) -> RefinedEntityRegistry<'ir> {
registry.map(|id, rough| self.refine_entity(id, rough.0, rough.1))
}

fn refine_type(&self, id: TypeId, rough: RoughType<'ast>) -> Type<'ir> {
match rough {
RoughType::Record(scope, r) => self.refine_record(id, scope, r),
RoughType::Enum(e) => self.refine_enum(id, e),
}
}

fn refine_entity(&self, id: TypeId, scope: ScopeId, e: &ast::Entity) -> RefinedType<'ir> {
fn refine_entity(&self, id: EntityId, scope: ScopeId, e: &ast::Entity) -> Entity<'ir> {
let ports = e.ports.iter().map(|port| Port {
kind: port.kind.0,
name: port.name,
ty: self.lookup_type(scope, &port.ty),
});

RefinedType::Entity(Entity {
Entity {
type_id: id,
name: e.name,
ports: self.arena.alloc_slice_fill_iter(ports),
})
}
}

fn refine_record(&self, id: TypeId, scope: ScopeId, r: &ast::Record) -> RefinedType<'ir> {
fn refine_record(&self, id: TypeId, scope: ScopeId, r: &ast::Record) -> Type<'ir> {
let fields = r.fields.iter().map(|field| Field {
name: field.name,
ty: self.lookup_type(scope, &field.ty),
});

RefinedType::Record(Record {
Type::Record(Record {
type_id: id,
name: r.name,
fields: self.arena.alloc_slice_fill_iter(fields),
})
}

fn refine_enum(&self, id: TypeId, e: &ast::Enum) -> RefinedType<'ir> {
fn refine_enum(&self, id: TypeId, e: &ast::Enum) -> Type<'ir> {
let varaints = e.variants.iter().map(|&ident| Variant { ident });

RefinedType::Enum(Enum {
Type::Enum(Enum {
type_id: id,
name: e.name,
variants: self.arena.alloc_slice_fill_iter(varaints),
Expand Down Expand Up @@ -109,6 +118,10 @@ impl<'ir, 'ast> RefineTypesStage<'ir, '_> {
return None;
}

(_, Some(Resolved::Entity(_))) => {
// TODO: better error, this can also appear at non-end position
MESSAGES.report(Message::wrong_path_end(segment, "Type", "Entity"))
}
(_, None) => {
MESSAGES.report(Message::could_not_resolve(segment));
return None;
Expand Down
51 changes: 51 additions & 0 deletions ohdlc/src/ir/stages/refine/registries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::{
ast::PortKind,
ir::registries::{EntityId, TypeId},
symbol::Ident,
};

#[derive(Debug)]
pub enum Type<'ir> {
Record(Record<'ir>),
Enum(Enum<'ir>),
}

#[derive(Debug)]
pub struct Entity<'ir> {
pub type_id: EntityId,
pub name: Ident,
pub ports: &'ir [Port],
}

#[derive(Debug)]
pub struct Port {
// TODO: we should agree on whether to use ast types in ir or ir types in ast, not both.
pub kind: PortKind,
pub name: Ident,
pub ty: Option<TypeId>,
}

#[derive(Debug)]
pub struct Record<'ir> {
pub type_id: TypeId,
pub name: Ident,
pub fields: &'ir [Field],
}

#[derive(Debug)]
pub struct Field {
pub name: Ident,
pub ty: Option<TypeId>,
}

#[derive(Debug)]
pub struct Enum<'ir> {
pub type_id: TypeId,
pub name: Ident,
pub variants: &'ir [Variant],
}

#[derive(Debug)]
pub struct Variant {
pub ident: Ident,
}
66 changes: 0 additions & 66 deletions ohdlc/src/ir/stages/refine_types/types.rs

This file was deleted.

Loading

0 comments on commit 10570c0

Please sign in to comment.