diff --git a/CHANGELOG.md b/CHANGELOG.md index 3673d53c..d8be3d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - The server will no longer send an LSP warning if dml-builtins is not found, instead relying on the client to perform this task - Fixed incorrect handling of explicit in-object constructs, correcting ranges of some error reports and detection of some linting errors - Fixed an issue where the server would sometimes report `progress end` too early +- Fixed reference-matching for `extern` declarations, goto-functionality around these declarations will now work correctly ## 0.9.20 - Made GetKnownContexts custom request not wait for analysis results by default, reducing the chances of overfilling workpool capacity while analysises are running. diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index fecfc586..1ef63398 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -983,6 +983,8 @@ impl DeviceAnalysis { self.symbol_info.method_symbols .get(m.location()) .and_then(|m|m.get(&parent.key)), + DMLShallowObjectVariant::Extern(e) => + self.symbol_info.variable_symbols.get(e.loc_span()), DMLShallowObjectVariant::Session(s) | DMLShallowObjectVariant::Saved(s) => self.symbol_info.variable_symbols.get(s.loc_span()), @@ -1110,7 +1112,6 @@ impl DeviceAnalysis { } } // TODO: types - // TODO: externs } fn lookup_global_from_ref(&self, @@ -1187,8 +1188,6 @@ impl DeviceAnalysis { }, // TODO: DMLType lookup DMLSymbolKind::Typedef => Ok(vec![]), - // TODO: Extern lookup - DMLSymbolKind::Extern => Ok(vec![]), e => { Err(format!("Unexpected symbol kind of global symbol: {:?}", e)) }, @@ -1200,8 +1199,7 @@ impl DeviceAnalysis { sym: &ContextedSymbol<'t>, limitations: &mut HashSet) -> Result, String> { - if matches!(sym.symbol.kind, DMLSymbolKind::Template | - DMLSymbolKind::Typedef | DMLSymbolKind::Extern) + if matches!(sym.symbol.kind, DMLSymbolKind::Template | DMLSymbolKind::Typedef) { return self.lookup_global_sym(sym.symbol); } @@ -2059,6 +2057,10 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, (vec![*constant.loc_span()], vec![*constant.loc_span()], vec![*constant.loc_span()]), + DMLShallowObjectVariant::Extern(e) => + (vec![*e.loc_span()], + vec![*e.loc_span()], + vec![*e.loc_span()]), DMLShallowObjectVariant::Session(data) | DMLShallowObjectVariant::Saved(data) => (vec![*data.declaration.loc_span()], @@ -2094,6 +2096,7 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, DMLShallowObjectVariant::Method(method_ref) => { internal_error!("Unreachable method_ref case reached, ignored. ({:?})", method_ref); }, + DMLShallowObjectVariant::Extern(_) | DMLShallowObjectVariant::Constant(_) | DMLShallowObjectVariant::Session(_) | DMLShallowObjectVariant::Saved(_) | diff --git a/src/analysis/parsing/structure.rs b/src/analysis/parsing/structure.rs index 56a984d3..a9aa648f 100644 --- a/src/analysis/parsing/structure.rs +++ b/src/analysis/parsing/structure.rs @@ -485,7 +485,30 @@ pub fn parse_vardecl(context: &ParseContext, stream: &mut FileParser<'_>, file_i } } -// session, saved, and extern +#[derive(Debug, Clone, PartialEq)] +pub struct ExternContent { + pub kind: LeafToken, + pub cdecl: CDecl, + pub semi: LeafToken, +} + +impl TreeElement for ExternContent { + fn range(&self) -> ZeroRange { + Range::combine( + self.kind.range(), + self.semi.range()) + } + fn subs(&self) -> TreeElements<'_> { + create_subs!(&self.kind, + &self.cdecl, + &self.semi) + } + fn post_parse_sanity(&self, _file: &TextFile) -> Vec { + self.cdecl.ensure_named() + } +} + +// session, saved #[derive(Debug, Clone, PartialEq)] pub struct VariableContent { pub kind: LeafToken, @@ -544,19 +567,11 @@ impl Parse for VariableContent { fn parse_extern( externtok: LeafToken, context: &ParseContext, stream: &mut FileParser<'_>, file_info: &FileInfo) -> DMLObject { - let (mut outer, mut inner) = object_contexts(context); - let cdecl = VarDecl::One(CDecl::parse(&inner, stream, file_info)); - let initializer = match inner.peek_kind(stream) { - Some(TokenKind::Assign) => { - let assign = inner.next_leaf(stream); - let initializer = Initializer::parse(&inner, stream, file_info); - Some((assign, initializer)) - }, - _ => None, - }; + let (mut outer, inner) = object_contexts(context); + let cdecl = CDecl::parse(&inner, stream, file_info); let semi = outer.expect_next_kind(stream, TokenKind::SemiColon); - DMLObjectContent::Extern(VariableContent { - kind: externtok, cdecl, initializer, semi + DMLObjectContent::Extern(ExternContent { + kind: externtok, cdecl, semi }).into() } @@ -1885,7 +1900,7 @@ pub enum DMLObjectContent { DMLVersion(DMLVersionContent), ErrorObject(ErrorObjectContent), Export(ExportContent), - Extern(VariableContent), + Extern(ExternContent), Event(CompositeObjectContent), Field(FieldContent), Footer(CBlockContent), diff --git a/src/analysis/structure/objects.rs b/src/analysis/structure/objects.rs index 502d1144..57f04156 100644 --- a/src/analysis/structure/objects.rs +++ b/src/analysis/structure/objects.rs @@ -647,6 +647,8 @@ impl SymbolContainer for DMLStatement { vec![con as &dyn StructureSymbol], DMLStatement::Object(DMLObject::Parameter(param)) => vec![param as &dyn StructureSymbol], + DMLStatement::Object(DMLObject::Extern(ext)) => + vec![ext as &dyn StructureSymbol], DMLStatement::HashIf(hashif) => hashif.symbols(), _ => vec![] } @@ -1343,6 +1345,38 @@ fn to_variable_decl_structure<'a>(content: &misc::CDecl, }) } +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Extern { + pub var: VariableDecl, + pub span: ZeroSpan, +} + +impl DMLNamed for Extern { + fn name(&self) -> &DMLString { + self.var.name() + } +} + +impl DeclarationSpan for Extern { + fn span(&self) -> &ZeroSpan { + &self.span + } +} + +impl StructureSymbol for Extern { + fn kind(&self) -> DMLSymbolKind { + DMLSymbolKind::Extern + } +} + +pub fn to_extern_structure<'a>(extrn: &structure::ExternContent, + report: &mut Vec, + file: FileSpec<'a>) -> Option { + let var = to_variable_decl_structure(&extrn.cdecl, VariableDeclKind::Extern, report, file)?; + let span = ZeroSpan::from_range(extrn.range(), file.path); + Some(Extern { var, span }) +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Variable { pub vars: Vec, @@ -1513,7 +1547,7 @@ pub enum DMLObject { Device(Device), Version(Version), Export(Export), - Extern(Variable), + Extern(Extern), Footer(CBlock), Header(CBlock), Hook(Hook), @@ -1613,10 +1647,7 @@ fn to_objectstatement<'a>(content: &structure::DMLObjectContent, Export::to_structure(con, report, file)?))), structure::DMLObjectContent::Extern(con) => DMLStatementKind::Statement(DMLStatement::Object(DMLObject::Extern( - to_variable_structure(&con.cdecl, - con.initializer.as_ref().map(|(_,i)|i), - VariableDeclKind::Extern, - report, file)?))), + to_extern_structure(con, report, file)?))), structure::DMLObjectContent::Event(con) => DMLStatementKind::Statement(DMLStatement::Object( DMLObject::CompositeObject( diff --git a/src/analysis/structure/toplevel.rs b/src/analysis/structure/toplevel.rs index 77a76079..5e6887da 100644 --- a/src/analysis/structure/toplevel.rs +++ b/src/analysis/structure/toplevel.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use crate::analysis::templating::evaluation::{Evaluatable, EvaluationContext}; use crate::logging::trace; -use crate::analysis::structure::objects::{Bitorder, CBlock, CompObjectKind, CompositeObject, Constant, DMLObject, DMLStatement, Device, Error, Export, Hook, Import, InEach, Instantiation, Loggroup, Method, MethodModifier, Parameter, Statements, Template, ToStructure, Typedef, Variable, Version, make_statements}; +use crate::analysis::structure::objects::{Bitorder, CBlock, CompObjectKind, CompositeObject, Constant, DMLObject, DMLStatement, Device, Error, Export, Extern, Hook, Import, InEach, Instantiation, Loggroup, Method, MethodModifier, Parameter, Statements, Template, ToStructure, Typedef, Variable, Version, make_statements}; use crate::analysis::structure::expressions::{Expression}; use crate::analysis::{DMLError, FileSpec, LocationRange}; use crate::analysis::parsing::tree::{ZeroRange, ZeroSpan, TreeElement}; @@ -394,6 +394,7 @@ pub struct StatementSpec { pub imports: Vec>, pub errors: Vec>, pub constants: Vec>, + pub externs: Vec, } impl SymbolContainer for StatementSpec { @@ -405,6 +406,7 @@ impl SymbolContainer for StatementSpec { symbols.append(&mut self.methods.to_symbols()); symbols.append(&mut self.constants.to_symbols()); symbols.append(&mut self.hooks.to_symbols()); + symbols.append(&mut self.externs.to_symbols()); symbols } } @@ -442,6 +444,7 @@ impl StatementSpec { saveds: vec![], params: vec![], constants: vec![], + externs: vec![], methods: vec![], hooks: vec![], instantiations: vec![], @@ -550,7 +553,7 @@ fn flatten_hashif_branch(context: StatementContext, DMLObject::Extern(ext) => report.push(LocalDMLError { range: - ext.vars.first().unwrap().object.name.span.range, + ext.var.object.name.span.range, description: format!( "Extern declaration not allowed in {}", context), @@ -729,6 +732,7 @@ fn flatten_hashif_branch(context: StatementContext, errors, templates, imports, + externs: vec![], } } @@ -744,7 +748,6 @@ pub struct TopLevel { pub bitorder: Option, pub loggroups: Vec, pub cblocks: Vec, - pub externs: Vec, pub typedefs: Vec, pub references: Vec, pub spec: StatementSpec, @@ -1007,7 +1010,7 @@ impl TopLevel { let spec = StatementSpec { objects, instantiations, ineachs, errors, imports, saveds, sessions, params, constants, methods, exports, hooks, - templates, + templates, externs, }; let mut references = vec![]; ast.references(&mut references, filespec); @@ -1026,7 +1029,6 @@ impl TopLevel { version, device, cblocks, - externs, typedefs, bitorder, loggroups, diff --git a/src/analysis/templating/objects.rs b/src/analysis/templating/objects.rs index ec3ffaff..b64727bf 100644 --- a/src/analysis/templating/objects.rs +++ b/src/analysis/templating/objects.rs @@ -11,18 +11,9 @@ use lsp_types::DiagnosticSeverity; use slotmap::{DefaultKey, Key, SlotMap}; use crate::utility::{partial_sort_by_key, partial_sort_by_key_in_place}; -use crate::analysis::{DMLNamed, DMLError}; +use crate::analysis::{DMLError, DMLNamed, Named}; use crate::analysis::parsing::tree::ZeroSpan; -use crate::analysis::structure::objects::{ArrayDim, CompObjectKind, - CompObjectKindDecl, - CompositeObject, - Constant, - DMLObjectCommon, Error, Hook, - Import, InEach, Initializer, - Instantiation, MaybeAbstract, Method, - Parameter, ParamValue, - Variable, - VariableDecl}; +use crate::analysis::structure::objects::{ArrayDim, CompObjectKind, CompObjectKindDecl, CompositeObject, Constant, DMLObjectCommon, Error, Extern, Hook, Import, InEach, Initializer, Instantiation, MaybeAbstract, Method, ParamValue, Parameter, Variable, VariableDecl}; use crate::analysis::structure::expressions::{DMLString, Value, ExpressionKind, Identifier, ConstantList}; use crate::analysis::structure::toplevel::{ExistCondition, ObjectDecl, @@ -59,6 +50,7 @@ pub struct ObjectSpec { pub in_eachs: InEachSpec, pub params: Vec>, pub constants: Vec>, + pub externs: Vec, pub saveds: Vec>, pub sessions: Vec>, pub errors: Vec>, @@ -296,6 +288,7 @@ fn create_spec<'t>(loc: ZeroSpan, errors: spec.errors.to_vec(), methods: spec.methods.to_vec(), hooks: spec.hooks.to_vec(), + externs: spec.externs.to_vec(), }.into() } @@ -498,6 +491,7 @@ impl CoarseObjectKind for DMLObject { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum DMLCoarseObjectKind { + Extern, CompositeObject, Method, Session, @@ -626,6 +620,8 @@ impl DMLHierarchyMember for DMLShallowObject { } fn kind(&self) -> DMLSymbolKind { match &self.variant { + DMLShallowObjectVariant::Extern(_) => + DMLSymbolKind::Extern, DMLShallowObjectVariant::Method(_) => DMLSymbolKind::Method, DMLShallowObjectVariant::Session(_) => @@ -644,6 +640,7 @@ impl DMLHierarchyMember for DMLShallowObject { #[derive(Clone, PartialEq, Eq)] pub enum DMLShallowObjectVariant { + Extern(Extern), Method(Arc), Session(DMLVariable), Saved(DMLVariable), @@ -655,6 +652,11 @@ pub enum DMLShallowObjectVariant { impl std::fmt::Debug for DMLShallowObjectVariant { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + DMLShallowObjectVariant::Extern(e) => f + .debug_struct("Extern") + .field("name", &e.var.name().val) + .field("location", e.loc_span()) + .finish(), DMLShallowObjectVariant::Method(m) => f .debug_struct("Method") .field("name", &m.identity()) @@ -729,6 +731,7 @@ impl CoarseObjectKind for DMLShallowObjectVariant { DMLShallowObjectVariant::Method(_) => DMLCoarseObjectKind::Method, DMLShallowObjectVariant::Session(_) => DMLCoarseObjectKind::Session, DMLShallowObjectVariant::Saved(_) => DMLCoarseObjectKind::Saved, + DMLShallowObjectVariant::Extern(_) => DMLCoarseObjectKind::Extern, DMLShallowObjectVariant::Parameter(_) => DMLCoarseObjectKind::Parameter, DMLShallowObjectVariant::Hook(_) => @@ -742,11 +745,12 @@ impl CoarseObjectKind for DMLShallowObjectVariant { impl DMLNamedMember for DMLShallowObjectVariant { fn identity(&self) -> &str { match self { - // TODO: probably these should themselve implement + // TODO: probably these should themselves implement // DMLNamedMember DMLShallowObjectVariant::Method(m) => m.identity(), DMLShallowObjectVariant::Session(d) | DMLShallowObjectVariant::Saved(d) => &d.declaration.name.val, + DMLShallowObjectVariant::Extern(e) => &e.var.name().val, DMLShallowObjectVariant::Parameter(p) => &p.identity, DMLShallowObjectVariant::Constant(c) => &c.name().val, DMLShallowObjectVariant::Hook(h) => &h.obj.name().val, @@ -761,6 +765,7 @@ impl DMLNamedMember for DMLShallowObjectVariant { DMLShallowObjectVariant::Parameter(_) => "parameter", DMLShallowObjectVariant::Constant(_) => "constant", DMLShallowObjectVariant::Hook(_) => "hook", + DMLShallowObjectVariant::Extern(_) => "extern", } } @@ -775,6 +780,7 @@ impl DMLNamedMember for DMLShallowObjectVariant { c.loc_span(), DMLShallowObjectVariant::Hook(h) => h.loc_span(), + DMLShallowObjectVariant::Extern(e) => e.loc_span(), } } } @@ -1188,6 +1194,13 @@ fn add_hooks(obj: &mut DMLCompositeObject, } } +fn add_externs(obj: &mut DMLCompositeObject, + externs: Vec<(ExistCondition, Extern)>) { + for (_, extrn) in externs { + obj.add_shallow_component(DMLShallowObjectVariant::Extern(extrn)); + } +} + fn add_constants(obj: &mut DMLCompositeObject, constants: Vec>) { for constant in constants { @@ -1566,14 +1579,21 @@ type HookMapping = HashMap)>)>; // Figure out symbol mappings for these specs // reports unguarded error // the hashmap is the symbol mapping -type CollectedSymbols = (HashMap)>, - Vec>, - SavedMapping, SessionMapping, - MethodMapping, HookMapping, ObjectMapping); +struct CollectedSymbols { + symbols: HashMap)>, + constants: Vec>, + saveds: SavedMapping, + sessions: SessionMapping, + methods: MethodMapping, + hooks: HookMapping, + subobjs: ObjectMapping, + externs: Vec<(ExistCondition, Extern)>, +} #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] enum CollisionKind { Constant, + Extern, Parameter, Subobject, Method, @@ -1808,6 +1828,7 @@ fn collect_symbols(parameters: &[DMLParameter], // them. Allows for stable sorting. let mut errors = vec![]; let mut constant_decls = vec![]; + let mut externs = vec![]; let mut saved_decls = vec![]; let mut session_decls = vec![]; let mut method_decls = vec![]; @@ -1827,6 +1848,11 @@ fn collect_symbols(parameters: &[DMLParameter], errors.push(error); } } + // Externs are unranked and cannot be spec-guarded, + // but inherit the condition of their spec. + for extrn in &spec.spec.externs { + externs.push((spec.condition.clone(), extrn.clone())); + } for constant in &spec.spec.constants { let condition = spec.declaration_condition(constant); if condition.exists(eval_context, report) { @@ -1835,6 +1861,7 @@ fn collect_symbols(parameters: &[DMLParameter], constant_decls.push((spec.spec.rank.clone(), constant)); } } + for saved in &spec.spec.saveds { let condition = spec.declaration_condition(saved); if condition.exists(eval_context, report) { @@ -1912,6 +1939,7 @@ fn collect_symbols(parameters: &[DMLParameter], severity: Some(DiagnosticSeverity::ERROR), }); } + for (_, constant) in constant_decls { constants.push(constant); } @@ -2004,6 +2032,16 @@ fn collect_symbols(parameters: &[DMLParameter], // Fully cross-check all declarations for collisions let mut collision_candidates = vec![]; + collision_candidates.extend(externs.iter().map(|(condition, extrn)| + CollisionCandidate { + name: extrn.var.name().val.clone(), + condition: condition.clone(), + span: extrn.var.name().span, + kind: CollisionKind::Extern, + logical_group: Some(CollisionGroup::Source( + extrn.var.name().span)), + })); + collision_candidates.extend(constants.iter().map(|constant| CollisionCandidate { name: constant.obj.name().val.clone(), @@ -2072,10 +2110,15 @@ fn collect_symbols(parameters: &[DMLParameter], // Map names to the most relevant declaration. Collision reporting is // handled separately below so it can check every declaration pair. // This creates an order for symbol definition precedence - // constant > parameter > subobj > method > saved > session > hook + // extern > constant > parameter > subobj > method > saved > session > hook let mut symbols: HashMap)> = HashMap::new(); + for (condition, extrn) in &externs { + symbols.entry(extrn.var.get_name()).or_insert_with(|| + (condition.clone(), *extrn.loc_span(), vec![])); + } + for constant in &constants { symbols.entry(constant.obj.name().val.clone()).or_insert_with(|| (constant.cond.clone(), *constant.loc_span(), vec![])); @@ -2136,7 +2179,16 @@ fn collect_symbols(parameters: &[DMLParameter], } report_collision_candidates(collision_candidates, report); - (symbols, constants, saveds, sessions, methods, hooks, subobjs) + CollectedSymbols { + symbols, + constants, + saveds, + sessions, + methods, + hooks, + subobjs, + externs, + } } fn merge_composite_subobj<'c>(name: String, @@ -2795,11 +2847,16 @@ fn make_object(loc: ZeroSpan, trace!("Parameters are: {:?}", parameters); - let (symbols, constants, - saveds, sessions, - methods, hooks, - subobjs) = collect_symbols(¶meters, &obj_specs, - eval_context, report); + let CollectedSymbols { + symbols, + constants, + saveds, + sessions, + methods, + hooks, + subobjs, + externs, + } = collect_symbols(¶meters, &obj_specs, eval_context, report); trace!("All local symbols are: {:?}", symbols.keys().map(|k|k.as_str()).collect::>()); @@ -2817,6 +2874,7 @@ fn make_object(loc: ZeroSpan, let new_obj = container.get_mut(new_obj_key).unwrap(); new_obj.used_ineach_locs = used_ineach_locs; add_templates(new_obj, &obj_specs); + add_externs(new_obj, externs); add_constants(new_obj, constants); add_parameters(new_obj, parameters); add_hooks(new_obj, hooks); diff --git a/tests/test_files/basic_lookup.dml b/tests/test_files/basic_lookup.dml index 38ad33a8..fcade129 100644 --- a/tests/test_files/basic_lookup.dml +++ b/tests/test_files/basic_lookup.dml @@ -140,3 +140,18 @@ method phantom_calls() { // @goto-ref[8]-> nonexistent_method(); // @goto-def-decl[5]-> nonexistent_bank.nonexistent_reg.read(); // @goto-def-decl[5]-> @goto-def-decl[22]-> @goto-def-decl[38]-> } + +// extern methods and values +// @loc[14]=extern_foo_decl +extern int (*foo)(bool); // @goto-ref[14]->extern_foo_call +// @loc[21]=extern_bar_decl +extern attr_value_t bar; // @goto-ref[21]->extern_bar_ref + +method refs_to_externs() { + // @loc[28]=extern_foo_call + // @goto-def-decl[28]->extern_foo_decl + local int foo_called = foo(false); + // @loc[36]=extern_bar_ref + // @goto-def-decl[36]->extern_bar_decl + local attr_value_t* bar_ref = &bar; +} diff --git a/tests/test_files/conditional_conflicts.dml b/tests/test_files/conditional_conflicts.dml index a99bf232..881da359 100644 --- a/tests/test_files/conditional_conflicts.dml +++ b/tests/test_files/conditional_conflicts.dml @@ -99,3 +99,8 @@ template exclusive_auto_assignment { } #else { is exclusive_auto_assignment; } + +extern int extern_int; +#if (false) { + session int extern_int; +}