Skip to content

Commit

Permalink
Get rid of FoundGlobal intermediary
Browse files Browse the repository at this point in the history
Fix #18
  • Loading branch information
VonTum committed Nov 1, 2024
1 parent 0c02b0f commit f789585
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 119 deletions.
2 changes: 1 addition & 1 deletion src/dev_aid/lsp/tree_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
NameElem: From<ID>,
{
let target_name_elem = NameElem::from(global.id);
self.visit(global.span, LocationInfo::Global(target_name_elem));
self.visit(global.total_span, LocationInfo::Global(target_name_elem));
for (id, template_arg) in global.template_args.iter_valids() {
let target_link_info = self.linker.get_link_info(target_name_elem).unwrap();
self.visit(
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl ErrorInfoObject for SubModuleInstance {
if let Some((name, span)) = &self.name {
(*span, format!("{name} declared here"))
} else {
(self.module_ref.span, "Used here".to_owned())
(self.module_ref.total_span, "Used here".to_owned())
}
}
}
Expand Down
178 changes: 74 additions & 104 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::parser::Cursor;
use super::*;

use crate::typing::template::{
GenerativeTemplateInputKind, TemplateAbstractTypes, TemplateArg, TemplateArgKind, TemplateArgs, TemplateInputKind
GenerativeTemplateInputKind, TemplateArg, TemplateArgKind, TemplateArgs, TemplateInputKind
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -23,21 +23,11 @@ enum NamedLocal {
TemplateType(TemplateID),
}

struct FoundGlobal {
global_id: NameElem,
total_span: Span,
written_template_args: TemplateArgs,
template_args_whole_span : Option<BracketSpan>,
}
impl FoundGlobal {
fn make_template_variables(&self, type_var_alloc: &mut UUIDAllocator<TypeVariableIDMarker>) -> TemplateAbstractTypes {
self.written_template_args.map(|_| AbstractType::Unknown(type_var_alloc.alloc()))
}
}

enum LocalOrGlobal {
Local(Span, NamedLocal),
Global(FoundGlobal),
Module(GlobalReference<ModuleUUID>),
Type(GlobalReference<TypeUUID>),
Constant(GlobalReference<ConstantUUID>),
// Error is already handled
NotFound(Span),
}
Expand Down Expand Up @@ -82,7 +72,7 @@ impl PartialWireReference {
let md = &ctx.globals[md_ref.id];
ctx.errors
.error(
md_ref.span,
md_ref.total_span,
format!(
"Expected a Wire Reference, but found module '{}' instead",
md.link_info.name
Expand Down Expand Up @@ -410,16 +400,35 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
};
if let Some((global_id, total_span)) = self.globals.resolve_global(*name_span) {
// MUST Still be at field!("template_args")
let template_args_whole_span = template_args_used.then(|| BracketSpan::from_outer(cursor.span()));
let template_span = template_args_used.then(|| BracketSpan::from_outer(cursor.span()));

let written_template_args = self.flatten_template_args(global_id, template_args_used, cursor);
let template_args = self.flatten_template_args(global_id, template_args_used, cursor);

LocalOrGlobal::Global(FoundGlobal {
global_id,
total_span,
written_template_args,
template_args_whole_span,
})
let template_arg_types = template_args.map(|_| AbstractType::Unknown(self.type_alloc.type_variable_alloc.alloc()));

match global_id {
NameElem::Module(id) => LocalOrGlobal::Module(GlobalReference {
id,
total_span,
template_args,
template_arg_types,
template_span,
}),
NameElem::Type(id) => LocalOrGlobal::Type(GlobalReference {
id,
total_span,
template_args,
template_arg_types,
template_span,
}),
NameElem::Constant(id) => LocalOrGlobal::Constant(GlobalReference {
id,
total_span,
template_args,
template_arg_types,
template_span,
}),
}
} else {
LocalOrGlobal::NotFound(*name_span)
}
Expand Down Expand Up @@ -479,34 +488,16 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
LocalOrGlobal::Local(span, NamedLocal::TemplateType(template_id)) => {
ModuleOrWrittenType::WrittenType(WrittenType::TemplateVariable(span, template_id))
}
LocalOrGlobal::Global(found_global) => match &found_global.global_id {
NameElem::Type(typ_id) => {
ModuleOrWrittenType::WrittenType(WrittenType::Named(GlobalReference {
template_arg_types: found_global.make_template_variables(&mut self.type_alloc.type_variable_alloc),
span: found_global.total_span,
id: *typ_id,
template_args: found_global.written_template_args,
template_span: found_global.template_args_whole_span,
}))
}
NameElem::Module(md_id) if ALLOW_MODULES => {
ModuleOrWrittenType::Module(GlobalReference {
template_arg_types: found_global.make_template_variables(&mut self.type_alloc.type_variable_alloc),
span: found_global.total_span,
id: *md_id,
template_args: found_global.written_template_args,
template_span: found_global.template_args_whole_span,
})
}
_ => {
self.globals.not_expected_global_error(
found_global.global_id,
found_global.total_span,
accepted_text,
);
ModuleOrWrittenType::WrittenType(WrittenType::Error(found_global.total_span))
}
},
LocalOrGlobal::Type(type_ref) => ModuleOrWrittenType::WrittenType(WrittenType::Named(type_ref)),
LocalOrGlobal::Module(module_ref) if ALLOW_MODULES => ModuleOrWrittenType::Module(module_ref),
LocalOrGlobal::Module(module_ref) => {
self.globals.not_expected_global_error(&module_ref, accepted_text);
ModuleOrWrittenType::WrittenType(WrittenType::Error(module_ref.total_span))
}
LocalOrGlobal::Constant(constant_ref) => {
self.globals.not_expected_global_error(&constant_ref, accepted_text);
ModuleOrWrittenType::WrittenType(WrittenType::Error(constant_ref.total_span))
}
LocalOrGlobal::NotFound(name_span) => {
ModuleOrWrittenType::WrittenType(WrittenType::Error(name_span))
} // Already covered
Expand Down Expand Up @@ -548,6 +539,20 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
}
}

fn alloc_submodule_instruction(&mut self, module_ref: GlobalReference<ModuleUUID>, name: Option<(String, Span)>, documentation: Documentation) -> FlatID {
let md = &self.globals[module_ref.id];
let local_interface_domains = md
.domain_names
.map(|_| DomainType::DomainVariable(self.type_alloc.domain_variable_alloc.alloc()));

self.instructions.alloc(Instruction::SubModule(SubModuleInstance{
name,
module_ref,
local_interface_domains,
documentation
}))
}

fn flatten_declaration<const ALLOW_MODULES: bool>(
&mut self,
declaration_context: DeclarationContext,
Expand Down Expand Up @@ -677,17 +682,7 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
}
let name = &self.globals.file_data.file_text[name_span];

let md = &self.globals[module_ref.id];
let local_interface_domains = md
.domain_names
.map(|_| DomainType::DomainVariable(self.type_alloc.domain_variable_alloc.alloc()));

let submod_id = self.instructions.alloc(Instruction::SubModule(SubModuleInstance{
name : Some((name.to_owned(), name_span)),
module_ref,
local_interface_domains,
documentation
}));
let submod_id = self.alloc_submodule_instruction(module_ref, Some((name.to_owned(), name_span)), documentation);

self.alloc_local_name(name_span, NamedLocal::SubModule(submod_id));

Expand Down Expand Up @@ -821,19 +816,8 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
PartialWireReference::Error => None,
PartialWireReference::GlobalModuleName(module_ref) => {
let documentation = cursor.extract_gathered_comments();
let interface_span = module_ref.span;
let md = &self.globals[module_ref.id];
let local_interface_domains = md
.domain_names
.map(|_| DomainType::DomainVariable(self.type_alloc.domain_variable_alloc.alloc()));

let submodule_decl =
self.instructions.alloc(Instruction::SubModule(SubModuleInstance {
name: None,
module_ref,
local_interface_domains,
documentation,
}));
let interface_span = module_ref.total_span;
let submodule_decl = self.alloc_submodule_instruction(module_ref, None, documentation);
Some(ModuleInterfaceReference {
submodule_decl,
submodule_interface: self.get_main_interface(submodule_decl, interface_span)?.0,
Expand Down Expand Up @@ -1000,34 +984,20 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
PartialWireReference::Error
}
},
LocalOrGlobal::Global(found_global) => {
match found_global.global_id {
NameElem::Constant(cst) => {
let root = WireReferenceRoot::NamedConstant(cst, expr_span);
PartialWireReference::WireReference(WireReference {
root,
is_generative: true,
path: Vec::new(),
})
}
NameElem::Module(md_id) => {
PartialWireReference::GlobalModuleName(GlobalReference {
template_arg_types: found_global.make_template_variables(&mut self.type_alloc.type_variable_alloc),
span: found_global.total_span,
id: md_id,
template_args: found_global.written_template_args,
template_span: found_global.template_args_whole_span,
})
}
NameElem::Type(_) => {
self.globals.not_expected_global_error(
found_global.global_id,
found_global.total_span,
"named wire: local or constant",
);
PartialWireReference::Error
}
}
LocalOrGlobal::Constant(cst_ref) => {
let root = WireReferenceRoot::NamedConstant(cst_ref.id, expr_span); // TODO Constants with templates
PartialWireReference::WireReference(WireReference {
root,
is_generative: true,
path: Vec::new(),
})
}
LocalOrGlobal::Module(md_ref) => {
PartialWireReference::GlobalModuleName(md_ref)
}
LocalOrGlobal::Type(type_ref) => {
self.globals.not_expected_global_error(&type_ref, "named wire: local or constant");
PartialWireReference::Error
}
LocalOrGlobal::NotFound(_) => PartialWireReference::Error
}
Expand Down Expand Up @@ -1070,7 +1040,7 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
match flattened_arr_expr {
PartialWireReference::Error => PartialWireReference::Error,
PartialWireReference::GlobalModuleName(md_ref) => {
self.errors.error(md_ref.span, "Ports or interfaces can only be accessed on modules that have been explicitly declared. Declare this submodule on its own line");
self.errors.error(md_ref.total_span, "Ports or interfaces can only be accessed on modules that have been explicitly declared. Declare this submodule on its own line");
PartialWireReference::Error
}
PartialWireReference::ModuleWithInterface { submodule_decl:_, submodule_name_span, interface:_, interface_name_span } => {
Expand Down
10 changes: 5 additions & 5 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Module {

pub fn get_instruction_span(&self, instr_id: FlatID) -> Span {
match &self.instructions[instr_id] {
Instruction::SubModule(sm) => sm.module_ref.span,
Instruction::SubModule(sm) => sm.module_ref.total_span,
Instruction::FuncCall(fc) => fc.whole_func_span,
Instruction::Declaration(decl) => decl.decl_span,
Instruction::Wire(w) => w.span,
Expand Down Expand Up @@ -421,10 +421,10 @@ pub enum WrittenType {
impl WrittenType {
pub fn get_span(&self) -> Span {
match self {
WrittenType::Error(span)
| WrittenType::TemplateVariable(span, ..)
| WrittenType::Named(GlobalReference { span, .. })
| WrittenType::Array(span, _) => *span,
WrittenType::Error(total_span)
| WrittenType::TemplateVariable(total_span, ..)
| WrittenType::Named(GlobalReference { total_span, .. })
| WrittenType::Array(total_span, _) => *total_span,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/flattening/typechecking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ pub fn apply_types(
type_checker.finalize_domain_type(domain_assigned_to_it_here);
}
for (_template_id, template_type) in &mut sm.module_ref.template_arg_types {
type_checker.finalize_abstract_type(types, template_type, sm.module_ref.span, errors);
type_checker.finalize_abstract_type(types, template_type, sm.module_ref.total_span, errors);
}
}
_other => {}
Expand Down
6 changes: 3 additions & 3 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {

if !check_all_template_args_valid(
&self.errors,
submod_instr.module_ref.span,
submod_instr.module_ref.total_span,
&sub_module.link_info,
&sm.template_args,
) {
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
let source_code_port = &sub_module.ports[port_id];
self.errors
.warn(
submod_instr.module_ref.span,
submod_instr.module_ref.total_span,
format!("Unused port '{}'", source_code_port.name),
)
.info_obj_different_file(
Expand Down Expand Up @@ -396,7 +396,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
sm.instance = Some(instance);
} else {
self.errors.error(
submod_instr.module_ref.span,
submod_instr.module_ref.total_span,
"Error instantiating submodule",
);
success = false;
Expand Down
8 changes: 5 additions & 3 deletions src/linker/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::ops::Index;

use crate::typing::template::GlobalReference;

use self::checkpoint::ResolvedGlobalsCheckpoint;

use super::*;
Expand Down Expand Up @@ -123,13 +125,13 @@ impl<'linker> GlobalResolver<'linker> {
pub fn get_linking_error_location(&self, name_elem: NameElem) -> LinkingErrorLocation {
self.linker.get_linking_error_location(name_elem)
}
pub fn not_expected_global_error(&self, name_elem: NameElem, span: Span, expected: &str) {
pub fn not_expected_global_error<ID: Copy>(&self, global_ref: &GlobalReference<ID>, expected: &str) where NameElem: From<ID> {
// SAFETY: The allocated linker objects aren't going to change.
let info = self.get_linking_error_location(name_elem);
let info = self.get_linking_error_location(NameElem::from(global_ref.id));
let name = &info.full_name;
let global_type = info.named_type;
let err_ref = self.errors.error(
span,
global_ref.total_span,
format!("{name} is not a {expected}, it is a {global_type} instead!"),
);
if let Some(span_file) = info.location {
Expand Down
2 changes: 1 addition & 1 deletion src/typing/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{flattening::WrittenType, linker::LinkInfo, value::TypedValue};

#[derive(Debug)]
pub struct GlobalReference<ID> {
pub span: Span,
pub total_span: Span,
pub id: ID,
pub template_args: TemplateArgs,
pub template_arg_types: TemplateAbstractTypes,
Expand Down

0 comments on commit f789585

Please sign in to comment.