Skip to content

Commit

Permalink
Submodules are now also typed with HM
Browse files Browse the repository at this point in the history
- Also finalized some other unneeded extra generative errors. Generative should be *fully* checked by flattening
  • Loading branch information
VonTum committed Oct 31, 2024
1 parent 0048e17 commit f617833
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 193 deletions.
3 changes: 1 addition & 2 deletions src/dev_aid/lsp/hover_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ pub fn hover(info: LocationInfo, linker: &Linker, file_data: &FileData) -> Vec<M
}
LocationInfo::Type(typ, link_info) => {
hover.sus_code(
typ.to_type()
.to_string(&linker.types, &link_info.template_arguments),
typ.to_string(&linker.types, &link_info.template_arguments),
);
}
LocationInfo::TemplateInput(in_obj, link_info, _template_id, template_arg) => {
Expand Down
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 @@ -295,7 +295,7 @@ impl<'linker, Visitor: FnMut(Span, LocationInfo<'linker>), Pruner: Fn(Span) -> b
(self.visitor)(typ_expr_span, LocationInfo::Type(typ_expr, link_info));
match typ_expr {
WrittenType::Error(_) => {}
WrittenType::Template(span, template_id) => {
WrittenType::TemplateVariable(span, template_id) => {
self.visit(
*span,
LocationInfo::TemplateInput(
Expand Down
98 changes: 57 additions & 41 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::alloc::{ArenaAllocator, UUIDAllocator, UUIDRange, UUID};
use crate::typing::abstract_type::DomainType;
use crate::typing::abstract_type::{AbstractType, DomainType};
use crate::{alloc::UUIDRangeIter, prelude::*};

use num::BigInt;
Expand All @@ -13,7 +13,7 @@ use super::parser::Cursor;
use super::*;

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

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -23,9 +23,21 @@ 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(NameElem, Span, TemplateArgs, Option<BracketSpan>),
Global(FoundGlobal),
// Error is already handled
NotFound(Span),
}
Expand Down Expand Up @@ -295,18 +307,20 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
.info_obj(link_info);
}

let template_arg = if cursor.optional_field(field!("val_arg")) {
let (template_arg, value_span) = if cursor.optional_field(field!("val_arg")) {
let value_span = cursor.span();
let (expr, is_generative) = self.flatten_expr(cursor);
if !is_generative {
self.errors.error(cursor.span(), "Template arguments must be known at compile-time!");
self.errors.error(value_span, "Template arguments must be known at compile-time!");
}
TemplateArgKind::Value(expr)
(TemplateArgKind::Value(expr), value_span)
} else if cursor.optional_field(field!("type_arg")) {
let value_span = cursor.span();
let typ = self.flatten_type(cursor);
TemplateArgKind::Type(typ)
(TemplateArgKind::Type(typ), value_span)
} else {
match self.local_variable_context.get_declaration_for(name) {
Some(NamedLocal::TemplateType(t)) => TemplateArgKind::Type(WrittenType::Template(name_span, t)),
(match self.local_variable_context.get_declaration_for(name) {
Some(NamedLocal::TemplateType(t)) => TemplateArgKind::Type(WrittenType::TemplateVariable(name_span, t)),
Some(NamedLocal::Declaration(decl_id)) => {
let wire_read_id = self.instructions.alloc(Instruction::Wire(WireInstance {
typ: self.type_alloc.alloc_unset_type(DomainAllocOption::Generative),
Expand All @@ -323,7 +337,7 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
self.errors.error(name_span, format!("{name} does not name a Type or a Value."));
return;
},
}
}, name_span)
};

if let Some((id, template_input)) = name_found {
Expand All @@ -336,7 +350,11 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
self.errors.error(name_span, format!("'{name}' has already been defined previously"))
.info_same_file(prev.name_span, "Defined here previously");
} else {
*elem = Some(TemplateArg { name_span, kind: template_arg });
*elem = Some(TemplateArg {
name_span,
value_span,
kind: template_arg
});
}
}
(TemplateInputKind::Type(_), TemplateArgKind::Value(_)) => {
Expand Down Expand Up @@ -390,18 +408,18 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
self.errors.todo(name_path[1], "Namespaces");
return LocalOrGlobal::NotFound(name_path[0]);
};
if let Some((found_global, found_global_span)) = self.globals.resolve_global(*name_span) {
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_arg_map = self.flatten_template_args(found_global, template_args_used, cursor);
let written_template_args = self.flatten_template_args(global_id, template_args_used, cursor);

LocalOrGlobal::Global(
found_global,
found_global_span,
template_arg_map,
LocalOrGlobal::Global(FoundGlobal {
global_id,
total_span,
written_template_args,
template_args_whole_span,
)
})
} else {
LocalOrGlobal::NotFound(*name_span)
}
Expand Down Expand Up @@ -459,37 +477,34 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
ModuleOrWrittenType::WrittenType(WrittenType::Error(span))
}
LocalOrGlobal::Local(span, NamedLocal::TemplateType(template_id)) => {
ModuleOrWrittenType::WrittenType(WrittenType::Template(span, template_id))
ModuleOrWrittenType::WrittenType(WrittenType::TemplateVariable(span, template_id))
}
LocalOrGlobal::Global(
resolved_global,
resolved_global_span,
template_args,
template_span,
) => match &resolved_global {
LocalOrGlobal::Global(found_global) => match &found_global.global_id {
NameElem::Type(typ_id) => {
ModuleOrWrittenType::WrittenType(WrittenType::Named(GlobalReference {
span: resolved_global_span,
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,
template_span,
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 {
span: resolved_global_span,
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,
template_span,
template_args: found_global.written_template_args,
template_span: found_global.template_args_whole_span,
})
}
_ => {
self.globals.not_expected_global_error(
resolved_global,
resolved_global_span,
found_global.global_id,
found_global.total_span,
accepted_text,
);
ModuleOrWrittenType::WrittenType(WrittenType::Error(resolved_global_span))
ModuleOrWrittenType::WrittenType(WrittenType::Error(found_global.total_span))
}
},
LocalOrGlobal::NotFound(name_span) => {
Expand Down Expand Up @@ -975,8 +990,8 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
PartialWireReference::Error
}
},
LocalOrGlobal::Global(name_elem, span, template_args, template_span) => {
match name_elem {
LocalOrGlobal::Global(found_global) => {
match found_global.global_id {
NameElem::Constant(cst) => {
let root = WireReferenceRoot::NamedConstant(cst, expr_span);
PartialWireReference::WireReference(WireReference {
Expand All @@ -987,16 +1002,17 @@ impl<'l, 'errs : 'l> FlatteningContext<'l, 'errs> {
}
NameElem::Module(md_id) => {
PartialWireReference::GlobalModuleName(GlobalReference {
span,
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,
template_span,
template_args: found_global.written_template_args,
template_span: found_global.template_args_whole_span,
})
}
NameElem::Type(_) => {
self.globals.not_expected_global_error(
name_elem,
span,
found_global.global_id,
found_global.total_span,
"named wire: local or constant",
);
PartialWireReference::Error
Expand Down
39 changes: 4 additions & 35 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::linker::{Documentation, LinkInfo};
use crate::{file_position::FileText, instantiation::InstantiationList, value::Value};

use crate::typing::{
abstract_type::{AbstractType, FullType},
template::{GlobalReference, TemplateArgs},
abstract_type::FullType,
template::GlobalReference,
};

#[derive(Debug)]
Expand Down Expand Up @@ -413,7 +413,7 @@ impl WireSource {
#[derive(Debug)]
pub enum WrittenType {
Error(Span),
Template(Span, TemplateID),
TemplateVariable(Span, TemplateID),
Named(GlobalReference<TypeUUID>),
Array(Span, Box<(WrittenType, FlatID, BracketSpan)>),
}
Expand All @@ -422,42 +422,11 @@ impl WrittenType {
pub fn get_span(&self) -> Span {
match self {
WrittenType::Error(span)
| WrittenType::Template(span, ..)
| WrittenType::TemplateVariable(span, ..)
| WrittenType::Named(GlobalReference { span, .. })
| WrittenType::Array(span, _) => *span,
}
}

pub fn to_type(&self) -> AbstractType {
match self {
WrittenType::Error(_) => AbstractType::Error,
WrittenType::Template(_, template_id) => AbstractType::Template(*template_id),
WrittenType::Named(named_type) => AbstractType::Named(named_type.id),
WrittenType::Array(_, arr_box) => {
let (elem_typ, _arr_idx, _br_span) = arr_box.deref();
AbstractType::Array(Box::new(elem_typ.to_type()))
}
}
}

pub fn to_type_with_substitute(&self, template_args: &TemplateArgs) -> AbstractType {
match self {
WrittenType::Error(_) => AbstractType::Error,
WrittenType::Template(_, template_id) => {
let Some(type_arg) = &template_args[*template_id] else {
return AbstractType::Error;
}; // Could not infer the type, though this is a TODO
let target_typ = type_arg.kind.unwrap_type();

target_typ.to_type()
}
WrittenType::Named(named_type) => AbstractType::Named(named_type.id),
WrittenType::Array(_, arr_box) => {
let (elem_typ, _arr_idx, _br_span) = arr_box.deref();
AbstractType::Array(Box::new(elem_typ.to_type_with_substitute(template_args)))
}
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit f617833

Please sign in to comment.