Skip to content

Commit

Permalink
Migrate declarations to use name_span instead of name_token
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Feb 24, 2024
1 parent 0e6b633 commit ca67a35
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ pub type SpanTypeExpression = (TypeExpression, Span);

#[derive(Debug)]
pub struct SignalDeclaration {
pub name_token : usize,
pub name_span : Span,
pub typ : SpanTypeExpression,
pub name : Box<str>,
pub identifier_type : IdentifierType,
pub latency_expr : Option<SpanExpression>
}
Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ fn handle_request(method : &str, params : serde_json::Value, file_cache : &mut L
LocationInfo::WireRef(md, decl_id) => {
let uri = file_cache.uris[md.link_info.file].clone();
let decl = md.flattened.instructions[decl_id].extract_wire_declaration();
let range = to_position_range(file_cache.linker.files[md.link_info.file].file_text.get_token_linechar_range(decl.name_token));
let range = to_position_range(file_cache.linker.files[md.link_info.file].file_text.get_span_linechar_range(decl.name_span));
GotoDefinitionResponse::Scalar(Location{uri, range})
}
LocationInfo::Temporary(_, _, _) => {
Expand Down
2 changes: 1 addition & 1 deletion src/dev_aid/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn walk_name_color(all_objects : &[NameElem], linker : &Linker) -> Vec<(IDEI
decl.typ_expr.for_each_located_type(&mut |_, span| {
result.push((IDEIdentifierType::Type, span));
});
result.push((IDEIdentifierType::Value(decl.identifier_type), Span::new_single_token(decl.name_token)));
result.push((IDEIdentifierType::Value(decl.identifier_type), decl.name_span));
}
Instruction::Write(conn) => {
let decl = module.flattened.instructions[conn.to.root].extract_wire_declaration();
Expand Down
1 change: 1 addition & 0 deletions src/file_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{fmt::Display, ops::{Index, Range}};
pub struct Span(usize, usize);

impl Span {
/// Only really used for having a span with the maximum size.
pub const MAX_POSSIBLE_SPAN : Span = Span(0, usize::MAX);

pub fn to_range<T : Clone>(&self, tokens : &[Range<T>]) -> Range<T> {
Expand Down
21 changes: 11 additions & 10 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct Declaration {
pub typ_expr : WrittenType,
pub typ : Type,
pub is_declared_in_this_module : bool,
pub name_token : usize,
pub name_span : Span,
pub name : Box<str>,
pub read_only : bool,
// If the program text already covers the write, then lsp stuff on this declaration shouldn't use it.
Expand All @@ -99,7 +99,7 @@ pub struct Declaration {

impl Declaration {
pub fn make_declared_here(&self, file : FileUUID) -> ErrorInfo {
error_info(Span::new_extend_to_include_token(self.typ_expr.get_span(), self.name_token), file, "Declared here")
error_info(Span::new_overarching(self.typ_expr.get_span(), self.name_span), file, "Declared here")
}
}

Expand Down Expand Up @@ -176,7 +176,7 @@ impl Instruction {
pub fn get_location_of_module_part(&self) -> Option<Span> {
match self {
Instruction::SubModule(sm) => sm.is_declared_in_this_module.then_some(sm.module_name_span),
Instruction::Declaration(decl) => decl.is_declared_in_this_module.then_some(Span::new_single_token(decl.name_token)),
Instruction::Declaration(decl) => decl.is_declared_in_this_module.then_some(decl.name_span),
Instruction::Wire(w) => w.is_declared_in_this_module.then_some(w.span),
Instruction::Write(conn) => conn.to.is_declared_in_this_module.then_some(conn.to.span),
Instruction::IfStatement(_) | Instruction::ForStatement(_) => None
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<'prev, 'inst, 'l, 'runtime> FlatteningContext<'prev, 'inst, 'l, 'runtime> {
match resolved.name_elem {
Some(NameElem::Module(id)) if ALLOW_MODULES => {
let md = &self.linker.get_module(id);
return self.alloc_module_interface(decl.name.clone(), md, id, decl.typ.1)
return self.alloc_module_interface(self.linker.file.file_text[decl.name_span].to_owned().into_boxed_str(), md, id, decl.typ.1)
}
Some(NameElem::Type(id)) => {
WrittenType::Named(decl.typ.1, id)
Expand All @@ -260,20 +260,21 @@ impl<'prev, 'inst, 'l, 'runtime> FlatteningContext<'prev, 'inst, 'l, 'runtime> {
};

let typ_expr_span = typ_expr.get_span();
let name = &self.linker.file.file_text[decl.name_span];
let inst_id = self.instructions.alloc(Instruction::Declaration(Declaration{
typ : typ_expr.to_type(),
typ_expr,
is_declared_in_this_module : self.is_declared_in_this_module,
read_only,
is_free_standing_decl,
identifier_type : decl.identifier_type,
name : decl.name.clone(),
name_token : decl.name_token,
name : name.to_owned().into_boxed_str(),
name_span : decl.name_span,
latency_specifier
}));

if let Err(conflict) = self.local_variable_context.add_declaration(&decl.name, inst_id) {
self.errors.error_with_info(Span::new_extend_to_include_token(typ_expr_span, decl.name_token), "This declaration conflicts with a previous declaration in the same scope", vec![self.instructions[conflict].extract_wire_declaration().make_declared_here(self.errors.file)])
if let Err(conflict) = self.local_variable_context.add_declaration(name, inst_id) {
self.errors.error_with_info(Span::new_overarching(typ_expr_span, decl.name_span), "This declaration conflicts with a previous declaration in the same scope", vec![self.instructions[conflict].extract_wire_declaration().make_declared_here(self.errors.file)])
}

inst_id
Expand Down Expand Up @@ -467,7 +468,7 @@ impl<'prev, 'inst, 'l, 'runtime> FlatteningContext<'prev, 'inst, 'l, 'runtime> {
}
LeftExpression::Declaration(decl) => {
let root = self.flatten_declaration::<true>(decl, false, !gets_assigned);
Some(ConnectionWrite{root, root_span : Span::new_single_token(decl.name_token), path: Vec::new(), span, is_declared_in_this_module: true})
Some(ConnectionWrite{root, root_span : decl.name_span, path: Vec::new(), span, is_declared_in_this_module: true})
}
}
}
Expand Down Expand Up @@ -857,7 +858,7 @@ impl<'prev, 'inst, 'l, 'runtime> FlatteningContext<'prev, 'inst, 'l, 'runtime> {
if !is_instance_used_map[id] {
if let Instruction::Declaration(decl) = inst {
if decl.is_declared_in_this_module {
self.errors.warn_basic(Span::new_single_token(decl.name_token), "Unused Variable: This variable does not affect the output ports of this module");
self.errors.warn_basic(decl.name_span, "Unused Variable: This variable does not affect the output ports of this module");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
LatencyCountingError::IndeterminablePortLatency { bad_ports } => {
for port in bad_ports {
let port_decl = self.flattened.instructions[self.wires[WireID::from_hidden_value(port.0)].original_wire].extract_wire_declaration();
self.errors.error_basic(Span::new_single_token(port_decl.name_token), format!("Cannot determine port latency. Options are {} and {}\nTry specifying an explicit latency or rework the module to remove this ambiguity", port.1, port.2));
self.errors.error_basic(port_decl.name_span, format!("Cannot determine port latency. Options are {} and {}\nTry specifying an explicit latency or rework the module to remove this ambiguity", port.1, port.2));
}
}
LatencyCountingError::ConflictingSpecifiedLatencies { conflict_path } => {
Expand Down
4 changes: 2 additions & 2 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ impl Linker {
}
None => {}
}
if decl.is_free_standing_decl && decl.name_token == token_idx {
location_builder.update(Span::new_single_token(decl.name_token), LocationInfo::WireRef(md, id));
if decl.is_free_standing_decl && decl.name_span.contains_token(token_idx) {
location_builder.update(decl.name_span, LocationInfo::WireRef(md, id));
}
}
Instruction::Wire(wire) => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<'file> ASTParserContext<'file> {
}

fn make_declaration(&mut self, type_expr : SpanTypeExpression, name : TokenContent, identifier_type : IdentifierType, latency_expr : Option<SpanExpression>) -> SignalDeclaration {
SignalDeclaration{typ : type_expr, name_token : name.tok_idx, name : self.file_text[name.text.clone()].into(), identifier_type, latency_expr}
SignalDeclaration{typ : type_expr, name_span : Span::new_single_token(name.tok_idx), identifier_type, latency_expr}
}

fn parse_identifier(&mut self, start_token_idx : usize, token_stream : &mut TokenStream) -> Span {
Expand Down

0 comments on commit ca67a35

Please sign in to comment.