diff --git a/CHANGELOG.md b/CHANGELOG.md index 63b7196a..f777a1ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,31 @@ # Change Log ## 0.9.23 +- The DLS now supports most type-level semantics and lookups +- - Goto-functionality from, and to, typedefs work +- - You can goto-type-def on `implement` definitions or references to find their interface typedef +- - Lookups to, and from, structure type members types (including interface types) work +- - Lookups through pointers and arrays types work (only on type level) +- - Lookup to type definitions from locals and method argument symbols and references to the same, generally work +- - Some conservative type-checking semantics are implemented, offering a minimum of feedback for assignment and overrides +- - Unknown types are reported, unless they are considered builtin +- - Various smaller semantics/errors are implemented, related to `bitfield` constructs, templates-as-types, and initializers - Fixed a case where the DLS would repeatedly cancel its own in-flight analysis when no import paths could be resolved - Fixed a parser crash when using utf-16 wide characters in documentation strings - Improved server handling of invalid character literals +- Fixed bug in template cycle detection that could cause crashes when an object + instantiated a template involved in a cycle +- Fixed bug in parser that could cause hangs when parsing broken 'layout' types +- Adjusted range for error report related to mismatching `memoized` methods +- The DLS will now report an error when a method override mismatches `startup` or `independent` qualifiers +- Adjusted range for error report related to overriding non-default methods +- The DLS will now report an error if it cannot determine a most-overriding method in an object +- The DLS will now report ambiguous parameter overrides, even if these are hidden below a valid override +- The DLS will now report an error if you declare multiple index variables of the same non-'_' name in a declaration +- The DLS will now report `error` objects using their error message, mimicking DMLC reports more closely +- Fixed the lookup of various symbols in nested scopes, including in method, switch, `#if`, and `after` scopes +- Reserved keywords are now correctly allowed as identifiers for several declarations +- The DLS will no longer report `error` objects in dead code ## 0.9.22 - Fixed an issue in server logging where the large allocations could be made by debug prints even at lower debug levels diff --git a/Cargo.toml b/Cargo.toml index c7d42a9b..3f133c6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,11 @@ name = "fuzzing" path = "tests/fuzzing.rs" required-features = ["fuzzing"] +[[test]] +name = "parser_recovery_tests" +path = "tests/parser_recovery_tests.rs" +required-features = ["fuzzing"] + [dependencies] anyhow = "1.0" clap = { version = "4.2", features = ["cargo", "derive"] } diff --git a/src/actions/mod.rs b/src/actions/mod.rs index f8426211..f9c2e431 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -9,7 +9,6 @@ use serde::Deserialize; use serde_json::json; use std::collections::{HashMap, HashSet}; -use std::cmp::Ordering; use std::path::{Path, PathBuf}; use std::fs; use std::sync::atomic::AtomicBool; @@ -555,11 +554,14 @@ impl InitActionContext { // when we have done linting but then turned off the setting lint.clear(); } - let files: HashSet<&PathBuf> = + let mut files: Vec<&PathBuf> = isolated.keys() .chain(device.keys()) .chain(lint.keys()) + .collect::>() + .into_iter() .collect(); + files.sort(); let direct_opens = self.direct_opens.lock().unwrap(); for file in files { let Some(canon_path) = CanonPath::from_path_buf(file.clone()) @@ -577,13 +579,22 @@ impl InitActionContext { .map(|e|e.with_source("dml-lint"))) .collect(); debug!("Reporting errors for {:?}", file); - // Sort by line - sorted_errors.sort_unstable_by( - |e1, e2|if e1.error.span.range > e2.error.span.range { - Ordering::Greater - } else { - Ordering::Less - }); + let severity_order = |severity|match severity { + Some(lsp_types::DiagnosticSeverity::ERROR) => 0, + Some(lsp_types::DiagnosticSeverity::WARNING) => 1, + Some(lsp_types::DiagnosticSeverity::INFORMATION) => 2, + Some(lsp_types::DiagnosticSeverity::HINT) => 3, + Some(_) => 4, + None => 5, + }; + sorted_errors.sort_unstable_by(|left, right| + left.error.span.range.cmp(&right.error.span.range) + .then_with(||severity_order(left.error.severity) + .cmp(&severity_order(right.error.severity))) + .then_with(||left.error.description + .cmp(&right.error.description)) + .then_with(||left.source.cmp(right.source)) + .then_with(||left.error.related.cmp(&right.error.related))); match parse_uri(file.to_str().unwrap()) { Ok(url) => notifier.notify_publish_diagnostics( PublishDiagnosticsParams::new( diff --git a/src/actions/requests.rs b/src/actions/requests.rs index 19be68b3..04aa6da3 100644 --- a/src/actions/requests.rs +++ b/src/actions/requests.rs @@ -14,7 +14,7 @@ use crate::actions::{AnalysisProgressKind, AnalysisWaitKind, ContextDefinition, InitActionContext, rpc_error_code}; use crate::actions::notifications::ContextDefinitionKindParam; -use crate::actions::semantic_lookup::{DLSLimitation, declarations_at_fp, definitions_at_fp, implementations_at_fp, references_at_fp}; +use crate::actions::semantic_lookup::{DLSLimitation, declarations_at_fp, definitions_at_fp, implementations_at_fp, references_at_fp, type_definitions_at_fp}; use crate::analysis::{Named, DeclarationSpan, LocationSpan}; use crate::analysis::symbols::SimpleSymbol; use crate::config::Config; @@ -32,6 +32,7 @@ pub use crate::lsp_data::request::{ GotoDeclaration, GotoDeclarationResponse, GotoDefinition, GotoImplementation, GotoImplementationResponse, + GotoTypeDefinition, GotoTypeDefinitionResponse, HoverRequest, RangeFormatting, References, @@ -157,6 +158,7 @@ fn structure_to_symbolkind(kind: DMLSymbolKind) DMLSymbolKind::Loggroup => SymbolKind::CONSTANT, DMLSymbolKind::Extern | + DMLSymbolKind::Field | DMLSymbolKind::Saved | DMLSymbolKind::Session | DMLSymbolKind::Local | @@ -570,6 +572,57 @@ impl RequestAction for GotoDefinition { } } +impl RequestAction for GotoTypeDefinition { + type Response = ResponseWithMessage>; + + fn timeout() -> std::time::Duration { + crate::server::dispatch::DEFAULT_REQUEST_TIMEOUT * 5 + } + + fn fallback_response() -> Result { + Ok(None.into()) + } + + fn get_identifier(params: &Self::Params) -> String { + Self::request_identifier( + &text_document_position_to_ident( + ¶ms.text_document_position_params)) + } + + fn handle( + ctx: InitActionContext, + params: Self::Params, + ) -> Result { + debug!("Requesting type definitions with params {:?}", params); + let fp = match ctx.text_doc_pos_to_pos( + ¶ms.text_document_position_params, + "goto_type_def") { + Some(fp) => fp, + None => return Self::fallback_response(), + }; + let canon_path = make_canon_path!(fp.path())?; + wait_for_device_path!(ctx, canon_path); + + let mut limitations = HashSet::new(); + match type_definitions_at_fp(&ctx, &fp, &mut limitations) { + Ok(locations) => { + let locations = locations.into_iter() + .map(|location|ls_util::dls_to_location(&location)) + .collect(); + Ok(response_maybe_with_limitations( + &fp.path(), + Some(GotoTypeDefinitionResponse::Array(locations)), + limitations, + &ctx)) + }, + Err(error) => { + warn_miss_lookup(error, fp.path().to_str()); + Self::fallback_response() + }, + } + } +} + impl RequestAction for References { type Response = ResponseWithMessage>; diff --git a/src/actions/semantic_lookup.rs b/src/actions/semantic_lookup.rs index 0499b193..f67398be 100644 --- a/src/actions/semantic_lookup.rs +++ b/src/actions/semantic_lookup.rs @@ -14,10 +14,11 @@ use crate::analysis::scope::{ContextedSymbol, ContextKey}; use crate::analysis::structure::objects::MaybeAbstract; use crate::analysis::structure::expressions::DMLString; use crate::analysis::symbols::DMLSymbolKind; +use crate::analysis::templating::types::TypeUse; use crate::analysis::{DeviceAnalysis, IsolatedAnalysis, LocationSpan, SymbolRef, ZeroRange}; use crate::analysis::parsing::tree::{ZeroSpan, ZeroFilePosition}; -use crate::analysis::reference::{CodeReference, Reference, ReferenceKind}; +use crate::analysis::reference::{CodeReference, Reference}; use crate::file_management::CanonPath; use crate::server::Output; @@ -42,11 +43,20 @@ impl fmt::Display for DLSLimitation { } } -pub fn type_semantic_limitation() -> DLSLimitation { +pub fn typeof_type_limitation() -> DLSLimitation { DLSLimitation { issue_num: 65, - description: "The DLS does not currently support semantic analysis of \ - types, including reference finding".to_string(), + description: "The DLS does not currently resolve 'typeof' types" + .to_string(), + } +} + +pub fn untracked_member_limitation() -> DLSLimitation { + DLSLimitation { + issue_num: 65, + description: "The DLS does not currently resolve members accessed \ + on expressions other than plain references, such as \ + function calls or casts".to_string(), } } @@ -133,6 +143,10 @@ impl <'t> SemanticLookup<'t> { Reference::CodeReference(code_ref) => { stored_symbols = get_symbols_of_ref(&code_ref, &analysis_info, &mut recognized_limitations); }, + Reference::UntrackedMember(_) => { + recognized_limitations.insert( + untracked_member_limitation()); + }, Reference::FileReference(file_ref) => { targeted_files = get_import_target_of(&file_ref, analysis_lock) .into_iter().cloned().collect(); @@ -203,11 +217,6 @@ fn get_refs_and_syms_at_fp<'t>( } return Ok(SymbolsOrReference::Symbols(syms)); } - if let Some(refr) = ref_at_pos.and_then(|r|r.as_code_ref()) { - if refr.reference_kind() == ReferenceKind::Type { - relevant_limitations.insert(type_semantic_limitation()); - } - } if let Some(refr) = ref_at_pos { Ok(SymbolsOrReference::Reference(refr.clone())) } else { @@ -459,3 +468,37 @@ pub fn references_at_fp(context: &InitActionContext, .flat_map(|s|s.lock().unwrap().references.clone()) .collect()) } + +pub fn type_definitions_at_fp( + context: &InitActionContext, + fp: &ZeroFilePosition, + relevant_limitations: &mut HashSet) + -> Result, AnalysisLookupError> { + let analysis_lock = context.analysis.lock().unwrap(); + let mut semantic_lookup = SemanticLookup::create_lookup( + fp, + &analysis_lock, + context)?; + mem::swap(relevant_limitations, &mut semantic_lookup.recognized_limitations); + + let mut locations = HashSet::new(); + for (device, symbols) in &semantic_lookup.stored_symbols { + for symbol in symbols { + let symbol = symbol.lock().unwrap(); + locations.extend(symbol.type_definitions.iter().copied()); + match symbol.typed { + Some(TypeUse::Declared(type_id)) => if let Some(declaration) = + device.types.declaration(type_id) { + locations.insert(declaration.name.span); + }, + Some(TypeUse::Anonymous(span)) => { + locations.insert(span); + }, + None => if symbol.typeof_declared { + relevant_limitations.insert(typeof_type_limitation()); + }, + } + } + } + Ok(locations.into_iter().collect()) +} diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index a520b299..6a6e45ee 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -43,13 +43,17 @@ pub use crate::analysis::parsing::tree:: {ZeroRange, ZeroSpan, ZeroPosition, ZeroFilePosition}; use crate::analysis::parsing::tree::{MissingToken, MissingContent, TreeElement}; -use crate::analysis::structure::objects::{CompObjectKind, Import, MaybeAbstract, ParamValue, Template}; -use crate::analysis::structure::statements::{ForPre, Statement, StatementKind}; -use crate::analysis::structure::toplevel::{ObjectDecl, TopLevel}; -use crate::analysis::structure::types::DMLType; +use crate::analysis::structure::objects::{CompObjectKind, Import, Loggroup, MaybeAbstract, ParamValue, Template}; +use crate::analysis::structure::statements::{ForPre, Statement, StatementKind, + SwitchCase}; +use crate::analysis::structure::toplevel::{ExistCondition, ObjectDecl, + StatementSpec, TopLevel}; +use crate::analysis::structure::types::{DMLType, DMLTypeKind, DMLTypeMember}; use crate::analysis::structure::expressions::{Expression, ExpressionKind, - DMLString}; -use crate::analysis::templating::objects::{make_device, DMLObject, + DMLString, MemberOp}; +use crate::analysis::templating::objects::{make_device, + check_method_body_types_for_objects, + DMLObject, DMLResolvedObject, DMLShallowObjectVariant, DMLCompositeObject, @@ -61,9 +65,11 @@ use crate::analysis::templating::topology::{RankMaker, rank_templates, create_templates_traits}; use crate::analysis::templating::methods::{DMLMethodArg, DMLMethodRef, DefaultCallReference, MethodDeclaration}; -use crate::analysis::templating::traits::{DMLTemplate, - TemplateTraitInfo}; -use crate::analysis::templating::types::DMLResolvedType; +use crate::analysis::templating::traits::{lookup_impl, DMLTemplate, + TemplateTraitInfo, TraitMemberKind}; +use crate::analysis::templating::types::{ + direct_members, DMLResolvedType, TypeDatabase, TypeId, TypeUse, +}; use crate::concurrency::AliveStatus; use crate::file_management::{PathResolver, CanonPath}; @@ -341,6 +347,11 @@ impl RangeEntry { #[derive(Debug, Clone, Default)] pub struct SymbolStorage { pub template_symbols: HashMap, + pub typedef_symbols: HashMap, + pub type_member_symbols: HashMap, + pub loggroup_symbols: HashMap, + // Member names of anonymous struct-like types, keyed by type span + pub anonymous_type_members: HashMap>, // Because some implicit parameters are defined in the same // place, we need to disambiguate this by name pub param_symbols: HashMap<(ZeroSpan, String), @@ -357,6 +368,9 @@ pub struct SymbolStorage { impl SymbolStorage { pub fn all_symbols<'a>(&'a self) -> impl Iterator { self.template_symbols.values() + .chain(self.typedef_symbols.values()) + .chain(self.type_member_symbols.values()) + .chain(self.loggroup_symbols.values()) .chain(self.param_symbols.values().flat_map(|h|h.values())) .chain(self.object_symbols.values()) .chain(self.method_symbols.values().flat_map(|h|h.values())) @@ -367,6 +381,8 @@ impl SymbolStorage { // This maps references to the symbol they reference, made as a lock // because we need to incrementally fill it as requests are made type ReferenceStorage = Arc>>>; +type ResolvedMethodCallCandidates = HashMap< + (StructureKey, ZeroSpan), Vec>>; // Analysis from the perspective of a particular DML device #[derive(Debug, Clone)] @@ -377,10 +393,16 @@ pub struct DeviceAnalysis { pub objects: StructureContainer, pub device_obj: DMLObject, pub templates: TemplateTraitInfo, + pub types: TypeDatabase, + // Non-colliding loggroup declarations by name; more than one only when + // their conditions are mutually exclusive + pub loggroups: HashMap>>, pub symbol_info: SymbolStorage, pub reference_info: ReferenceStorage, pub template_object_implementation_map: HashMap>, + // Name spans of object and method declarations guarded by '#if' + pub conditional_declarations: HashSet, pub path: CanonPath, pub dependant_files: Vec, pub clientpath: PathBuf, @@ -563,9 +585,16 @@ impl ReferenceCache { fn flatten_ref(refr: &NodeRef, agn: &mut Vec) { match refr { NodeRef::Simple(dmlstring) => agn.push(dmlstring.val.clone()), - NodeRef::Sub(sub, dmlstring, _) => { + NodeRef::Sub(sub, op, dmlstring, _) => { Self::flatten_ref(sub.as_ref(), agn); - agn.push(dmlstring.val.clone()); + agn.push(match op { + MemberOp::Dot => dmlstring.val.clone(), + MemberOp::Arrow => format!("->{}", dmlstring.val), + }); + }, + NodeRef::Index(inner) => { + Self::flatten_ref(inner.as_ref(), agn); + agn.push("[]".to_string()); }, } } @@ -876,12 +905,21 @@ impl DeviceAnalysis { let (first, rest) = context_chain.split_first().unwrap(); let next_objs = match first { ContextKey::Structure(sym) => { - let res: Vec<_> = curr_obj.get_object(sym.name_ref()).cloned() - .into_iter().collect(); - if res.is_empty() { - return Err(format!("Failed to find structural object corresponding to {:?} at {:?}", first, curr_obj.identity())); + match curr_obj.get_object(sym.name_ref()) { + Some(found_obj) => if found_obj.resolve(&self.objects) + .as_comp().is_some() { + vec![found_obj.clone()] + } else { + // A higher-precedence non-composite declaration + // won the name collision + return Ok(vec![]); + }, + None if self.conditional_declarations + .contains(sym.loc_span()) => return Ok(vec![]), + None => return Err(format!( + "Failed to find structural object corresponding to \ + {:?} at {:?}", first, curr_obj.identity())), } - res }, ContextKey::Method(sym) => { if let Some(found_obj) @@ -899,14 +937,22 @@ impl DeviceAnalysis { return Ok(vec![]); } } - else { - // If it is an error to not find an result, handle - // it higher up in stack - return Err(format!("Failed to find method object corresponding to {:?} at {:?}", first, curr_obj.identity())); + else if self.conditional_declarations + .contains(sym.loc_span()) { + return Ok(vec![]); + } else { + return Err(format!( + "Failed to find method object corresponding to \ + {:?} at {:?}", first, curr_obj.identity())); } }, ContextKey::Template(sym) => if let Some(templ) = self.templates.templates.get(sym.name_ref()) { + // A discarded duplicate declaration has no instances + if templ.location.as_ref().is_some_and( + |location|location != sym.loc_span()) { + return Ok(vec![]); + } if let Some(templ_impls) = templ.location.as_ref().and_then( |loc|self @@ -1077,7 +1123,8 @@ impl DeviceAnalysis { // TODO: Can we do _anything_ here? Perhaps defer to the // default value (if any)? DMLShallowObjectVariant::Session(_) | - DMLShallowObjectVariant::Saved(_) => (), + DMLShallowObjectVariant::Saved(_) | + DMLShallowObjectVariant::Extern(_) => (), // Special case for hooks, 'send_now' is the only currently // allowed member DMLShallowObjectVariant::Hook(_) => if name == "send_now" { @@ -1100,6 +1147,15 @@ impl DeviceAnalysis { ref_matches: &mut ReferenceMatches) { if let NodeRef::Simple(name) = node { + for loggroup in self.loggroups.get(&name.val) + .into_iter().flatten() { + if let Some(symbol) = self.symbol_info.loggroup_symbols + .get(&loggroup.obj.name.span) { + ref_matches.add_match(Arc::clone(symbol)); + } else { + error!("Unexpectedly missing a loggroup {}", name.val); + } + } if let Some(templ) = self.templates.templates.get( name.val.as_str()) { if let Some(templ_loc) = &templ.location { @@ -1113,7 +1169,6 @@ impl DeviceAnalysis { } } } - // TODO: types } fn lookup_global_from_ref(&self, @@ -1137,8 +1192,24 @@ impl DeviceAnalysis { } } }, - // TODO: type lookup - ReferenceKind::Type => (), + ReferenceKind::Type => { + for type_id in self.types.typedefs_named(&reference.name) { + if let Some(declaration) = self.types.declaration(*type_id) { + let symbols = if declaration.underlying.is_some() { + self.symbol_info.typedef_symbols.get(type_id) + } else { + self.symbol_info.template_symbols + .get(&declaration.declaration_span) + }; + if let Some(symbol) = symbols { + ref_matches.add_match(Arc::clone(symbol)); + } else { + error!("Unexpectedly missing type symbol {}", + reference.name); + } + } + } + }, _ => error!("Invalid global reference kind in {:?}", reference), } ref_matches @@ -1162,6 +1233,15 @@ impl DeviceAnalysis { sym: &SimpleSymbol, ref_matches: &mut ReferenceMatches) { let resolved = obj.resolve(&self.objects); + // Method-local declarations are identified by their location; a + // name lookup would also hit same-named locals in other scopes and + // the overriding method's locals + if matches!(sym.kind, DMLSymbolKind::Local | DMLSymbolKind::MethodArg) { + if let Some(symbol) = self.symbol_info.variable_symbols.get(&sym.loc) { + ref_matches.add_match(Arc::clone(symbol)); + return; + } + } if resolved.as_comp().is_some_and(|c|c.kind == CompObjectKind::Device) && sym.kind == DMLSymbolKind::Template { self.lookup_global_symbol(sym, ref_matches); @@ -1188,8 +1268,30 @@ impl DeviceAnalysis { Err(format!("Unexpectedly missing template matching {:?}", sym)) } }, - // TODO: DMLType lookup - DMLSymbolKind::Typedef => Ok(vec![]), + DMLSymbolKind::Typedef => { + let matches = self.types.typedefs_named(sym.name.as_str()) + .iter() + .filter(|type_id|self.types.declaration(**type_id) + .is_some_and(|decl|decl.name.span == sym.loc)) + .filter_map(|type_id|self.symbol_info.typedef_symbols + .get(type_id).map(Arc::clone)) + .collect::>(); + if matches.is_empty() { + Err(format!("Unexpectedly missing typedef matching {:?}", sym)) + } else { + Ok(matches) + } + }, + DMLSymbolKind::Field => self.symbol_info.type_member_symbols + .get(&sym.loc) + .map(|symbol|vec![Arc::clone(symbol)]) + .ok_or_else(||format!( + "Unexpectedly missing type member matching {:?}", sym)), + DMLSymbolKind::Loggroup => self.symbol_info.loggroup_symbols + .get(&sym.loc) + .map(|symbol|vec![Arc::clone(symbol)]) + .ok_or_else(||format!( + "Unexpectedly missing loggroup matching {:?}", sym)), e => { Err(format!("Unexpected symbol kind of global symbol: {:?}", e)) }, @@ -1201,7 +1303,11 @@ impl DeviceAnalysis { sym: &ContextedSymbol<'t>, limitations: &mut HashSet) -> Result, String> { - if matches!(sym.symbol.kind, DMLSymbolKind::Template | DMLSymbolKind::Typedef) + if matches!(sym.symbol.kind, + DMLSymbolKind::Template | + DMLSymbolKind::Typedef | + DMLSymbolKind::Field | + DMLSymbolKind::Loggroup) { return self.lookup_global_sym(sym.symbol); } @@ -1235,10 +1341,45 @@ impl DeviceAnalysis { fn resolve_noderef_in_symbol<'t>(&'t self, symbol: &'t SymbolRef, - node: &NodeRef, + member_name: &DMLString, + indices: u32, + op: &MemberOp, method_structure: &HashMap, ref_matches: &mut ReferenceMatches) { + let (typed, indirections) = { + let sym = symbol.lock().unwrap(); + (sym.typed, sym.indirections) + }; + if let Some((core, extra)) = typed.and_then( + |typed|self.types.struct_core(typed)) { + // '.' needs the struct itself and '->' a pointer to (or array of) it + let dereferences = indices + u32::from(*op == MemberOp::Arrow); + if indirections + extra == dereferences { + for span in self.struct_member_spans(core, &member_name.val) { + if let Some(member_symbol) = self.symbol_info + .type_member_symbols.get(&span) { + ref_matches.add_match(Arc::clone(member_symbol)); + } + } + } + return; + } + if let Some(TypeUse::Declared(type_id)) = typed { + if let Some(declaration) = self.types.declaration(type_id) + .filter(|declaration|declaration.underlying.is_none()) { + // Template types expose the members of their trait + if indirections == indices && *op == MemberOp::Dot { + for member_symbol in self.trait_member_symbols( + &declaration.name.val, &member_name.val) { + ref_matches.add_match(member_symbol); + } + } + return; + } + } + let node = NodeRef::Simple(member_name.clone()); + let node = &node; let sym = symbol.lock().unwrap(); match &sym.source { SymbolSource::DMLObject(obj) => { @@ -1257,10 +1398,61 @@ impl DeviceAnalysis { // TODO: Cannot be resolved without constant folding SymbolSource::MethodArg(_method, _name) => (), SymbolSource::MethodLocal(_method, _name) => (), - // TODO: Fix once type system is sorted - SymbolSource::Type(_typed) => (), - // TODO: Handle lookups inside templates - SymbolSource::Template(_templ) => (), + // NOTE: types handled near start-of-function + SymbolSource::Type(_) => (), + SymbolSource::TypeMember(_, _) => (), + SymbolSource::Template(_) => (), + SymbolSource::Loggroup(_) => (), + } + } + + // Symbols of the declaration that the trait of 'template' exposes as + // 'member', across all objects instantiating it + fn trait_member_symbols(&self, template: &str, member: &str) + -> Vec { + let Some(owner) = self.templates.traits.get(template) + .and_then(|template_trait|lookup_impl(template_trait, &member)) + else { + return vec![]; + }; + let Some(declaration) = owner.get_member(member) else { + return vec![]; + }; + let location = *declaration.location(); + match declaration { + TraitMemberKind::Method(_) => self.symbol_info.method_symbols + .get(&location).into_iter() + .flat_map(|symbols|symbols.values()).map(Arc::clone) + .collect(), + TraitMemberKind::Session(_) | TraitMemberKind::Saved(_) => + self.symbol_info.variable_symbols.get(&location) + .map(Arc::clone).into_iter().collect(), + TraitMemberKind::Parameter(_) => self.symbol_info.param_symbols + .iter() + .filter(|((_, name), _)|name == member) + .flat_map(|(_, symbols)|symbols.values()) + .filter(|symbol|symbol.lock().unwrap().declarations + .contains(&location)) + .map(Arc::clone) + .collect(), + } + } + + fn struct_member_spans(&self, core: TypeUse, name: &str) -> Vec { + match core { + TypeUse::Declared(type_id) => self.types.declaration(type_id) + .and_then(|declaration|declaration.underlying.as_ref()) + .and_then(direct_members) + .into_iter().flatten() + .filter_map(|member|member.name.as_ref()) + .filter(|member_name|member_name.val == name) + .map(|member_name|member_name.span) + .collect(), + TypeUse::Anonymous(span) => self.symbol_info + .anonymous_type_members.get(&span).into_iter().flatten() + .filter(|member_name|member_name.val == name) + .map(|member_name|member_name.span) + .collect(), } } @@ -1396,6 +1588,79 @@ impl DeviceAnalysis { } } + // Handles 'templates.T' and 'templates.T.m', optionally prefixed by + // 'this.'; returns false if 'node' is not of that form + fn resolve_template_qualified(&self, + parent_key: &StructureKey, + node: &NodeRef, + ref_matches: &mut ReferenceMatches) -> bool { + fn is_templates(node: &NodeRef) -> bool { + match node { + NodeRef::Simple(name) => name.val == "templates", + NodeRef::Sub(base, MemberOp::Dot, name, _) => + name.val == "templates" && matches!( + base.as_ref(), + NodeRef::Simple(this) if this.val == "this"), + _ => false, + } + } + let (template_name, member) = match node { + NodeRef::Sub(root, MemberOp::Dot, template, _) + if is_templates(root) => (template, None), + NodeRef::Sub(qualified, MemberOp::Dot, member, _) => + match qualified.as_ref() { + NodeRef::Sub(root, MemberOp::Dot, template, _) + if is_templates(root) => (template, Some(member)), + _ => return false, + }, + _ => return false, + }; + let Some(template) = self.templates.templates.get(&template_name.val) + else { + return false; + }; + let Some(member) = member else { + if let Some(symbol) = template.location.as_ref().and_then( + |location|self.symbol_info.template_symbols.get(location)) { + ref_matches.add_match(Arc::clone(symbol)); + } + return true; + }; + let Some(method) = self.objects.get(*parent_key) + .and_then(|object|object.get_method(member.val.as_str())) else { + return true; + }; + // The implementation 'T' sees is the first one, in override order, + // declared in 'T' or a template it instantiates + let mut spans = vec![]; + let mut pending = vec![template]; + while let Some(next) = pending.pop() { + if spans.contains(&next.spec.span) { + continue; + } + spans.push(next.spec.span); + pending.extend(next.spec.instantiations.values().flatten()); + } + fn find<'m>(method: &'m Arc, + spans: &[ZeroSpan]) -> Option<&'m Arc> { + if spans.iter().any(|span|span.contains_pos( + &method.location().start_position())) { + return Some(method); + } + match method.get_default() { + Some(DefaultCallReference::Valid(next)) => find(next, spans), + Some(DefaultCallReference::Ambiguous(nexts)) => + nexts.iter().find_map(|next|find(next, spans)), + _ => None, + } + } + if let Some(symbol) = find(&method, &spans) + .and_then(|found|self.get_method_symbol(found, parent_key)) { + ref_matches.add_match(Arc::clone(symbol)); + } + true + } + fn resolve_noderef_in_method<'c>(&'c self, parent_key: &StructureKey, method: &Arc, @@ -1403,6 +1668,9 @@ impl DeviceAnalysis { method_structure: &HashMap, ref_matches: &mut ReferenceMatches) { + if self.resolve_template_qualified(parent_key, node, ref_matches) { + return; + } match node { NodeRef::Simple(simple) => { self.resolve_simple_noderef_in_method(parent_key, method, simple, @@ -1410,26 +1678,30 @@ impl DeviceAnalysis { method_structure, ref_matches); }, - NodeRef::Sub(subnode, simple, _) => { + NodeRef::Sub(subnode, op, simple, _) => { + let (base, indices) = subnode.strip_indices(); let mut intermediate_matches = ReferenceMatches::new(); self.resolve_noderef_in_method(parent_key, method, - subnode, + base, method_structure, &mut intermediate_matches); if let Some(syms) = intermediate_matches.as_matches() { - let wrapped_simple = NodeRef::Simple(simple.clone()); for sym in syms { self.resolve_noderef_in_symbol( &sym, - &wrapped_simple, + simple, + indices, + op, method_structure, ref_matches); } } else { ref_matches.merge_with(intermediate_matches); } - } + }, + NodeRef::Index(inner) => self.resolve_noderef_in_method( + parent_key, method, inner, method_structure, ref_matches), } } @@ -1440,6 +1712,13 @@ impl DeviceAnalysis { RangeEntry>, ref_matches: &mut ReferenceMatches) { trace!("Resolving noderef {:?} in obj {:?}", node, obj); + let parent_key = match obj.resolve(&self.objects) { + DMLResolvedObject::CompObject(object) => object.key, + DMLResolvedObject::ShallowObject(shallow) => shallow.parent, + }; + if self.resolve_template_qualified(&parent_key, node, ref_matches) { + return; + } match node { NodeRef::Simple(simple) => { let resolvedobj = obj.resolve(&self.objects); @@ -1462,26 +1741,30 @@ impl DeviceAnalysis { ref_matches); } }, - NodeRef::Sub(subnode, simple, _) => { + NodeRef::Sub(subnode, op, simple, _) => { + let (base, indices) = subnode.strip_indices(); let mut intermediate_matches = ReferenceMatches::new(); self.resolve_noderef_in_obj(obj, - subnode, + base, method_structure, &mut intermediate_matches); if let Some(syms) = intermediate_matches.as_matches() { - let wrapped_simple = NodeRef::Simple(simple.clone()); for sym in syms { self.resolve_noderef_in_symbol( &sym, - &wrapped_simple, + simple, + indices, + op, method_structure, ref_matches); } } else { ref_matches.merge_with(intermediate_matches); } - } + }, + NodeRef::Index(inner) => self.resolve_noderef_in_obj( + obj, inner, method_structure, ref_matches), } } @@ -1580,6 +1863,7 @@ impl DeviceAnalysis { report: &mut Vec, method_structure: &HashMap, reference_cache: &Mutex, + resolved_method_calls: &Mutex, status: &AliveStatus) -> AnalysisProcessResult<()> { if scope_chain.is_empty() { internal_error!("Attempted to match references in empty scope chain"); @@ -1651,6 +1935,18 @@ impl DeviceAnalysis { ReferenceMatchKind::Found => for symbol in &symbol_lookup.references { Self::handle_symbol_ref(symbol, code_ref); + let caller = match object { + DMLObject::CompObject(caller) => *caller, + DMLObject::ShallowObject(caller) => + caller.parent, + }; + let symbol = symbol.lock().unwrap(); + let SymbolSource::Method(_, method) = + &symbol.source + else { continue; }; + resolved_method_calls.lock().unwrap() + .entry((caller, *code_ref.loc_span())) + .or_default().push(Arc::clone(method)); }, ReferenceMatchKind::MismatchedFind => //TODO: report mismatch, @@ -1892,6 +2188,7 @@ impl IsolatedAnalysis { fn objects_to_symbols(maker: &SymbolMaker, objects: &StructureContainer, + types: &TypeDatabase, errors: &mut Vec, method_structure: &mut HashMap ) -> SymbolStorage { @@ -1907,6 +2204,7 @@ fn objects_to_symbols(maker: &SymbolMaker, if let DMLObject::ShallowObject(shallow) = subobj { add_new_symbol_from_shallow(maker, shallow, + types, errors, &mut storage, method_structure); @@ -1932,10 +2230,18 @@ fn template_to_symbol(maker: &SymbolMaker, fn extend_with_templates(maker: &SymbolMaker, storage: &mut SymbolStorage, - templates: &TemplateTraitInfo) { + templates: &TemplateTraitInfo, + types: &TypeDatabase) { for template in templates.templates.values() { if let Some(new_templ) = template_to_symbol(maker, template) { let loc = new_templ.lock().unwrap().loc; + let typed = types + .typedefs_named(&template.name) + .iter() + .find(|type_id|types.declaration(**type_id) + .is_some_and(|declaration|declaration.underlying.is_none())) + .map(|type_id|TypeUse::Declared(*type_id)); + new_templ.lock().unwrap().typed = typed; if let Some(prev) = storage.template_symbols .insert(loc, new_templ) { internal_error!("Unexpectedly two template symbols @@ -1947,6 +2253,214 @@ fn extend_with_templates(maker: &SymbolMaker, } } +fn extend_with_loggroups(maker: &SymbolMaker, + storage: &mut SymbolStorage, + bases: &[IsolatedAnalysis]) { + for loggroup in bases.iter() + .flat_map(|base|&base.toplevel.loggroups) { + let name = &loggroup.obj.name; + storage.loggroup_symbols.insert(name.span, symbol_ref!( + maker, + name.span, + DMLSymbolKind::Loggroup, + SymbolSource::Loggroup(name.clone()), + definitions = vec![name.span], + declarations = vec![name.span] + )); + } +} + +// Later declarations are reported as collisions unless their condition +// excludes every earlier one with the same name +fn collect_loggroups(bases: &[IsolatedAnalysis], + errors: &mut Vec) + -> HashMap>> { + let mut loggroups: Vec<&ObjectDecl> = bases.iter() + .flat_map(|base|&base.toplevel.loggroups).collect(); + loggroups.sort_by_key(|loggroup|loggroup.obj.name.span); + let mut unique: HashMap>> + = HashMap::default(); + for loggroup in loggroups { + let name = &loggroup.obj.name; + let previous = unique.entry(name.val.clone()).or_default(); + if let Some(collision) = previous.iter().find( + |other|!loggroup.cond.guaranteed_excluded_from(&other.cond)) { + errors.push(DMLError { + span: name.span, + description: format!("Duplicate loggroup name; '{}'", + name.val), + severity: Some(DiagnosticSeverity::ERROR), + related: vec![(collision.obj.name.span, + "Previously defined here".to_string())], + }); + } else { + previous.push(loggroup.clone()); + } + } + unique +} + +fn extend_with_types(maker: &SymbolMaker, + storage: &mut SymbolStorage, + types: &TypeDatabase) { + for declaration in types.declarations() { + let Some(underlying) = declaration.underlying.as_ref() else { + continue; + }; + let symbol = symbol_ref!( + maker, + declaration.name.span, + DMLSymbolKind::Typedef, + SymbolSource::Type(declaration.id), + definitions = vec![declaration.name.span], + declarations = vec![declaration.name.span] + ); + symbol.lock().unwrap().typed = Some(TypeUse::Declared(declaration.id)); + storage.typedef_symbols.insert(declaration.id, symbol); + match direct_members(underlying) { + Some(members) => add_member_symbols( + maker, types, TypeUse::Declared(declaration.id), members, + &mut storage.anonymous_type_members, + &mut storage.type_member_symbols), + None => register_anonymous_types( + maker, types, underlying, + &mut storage.anonymous_type_members, + &mut storage.type_member_symbols), + } + } +} + +fn set_symbol_type(symbol: &SymbolRef, types: &TypeDatabase, typed: &DMLType) { + let shape = types.resolve_type_shape(typed); + let mut symbol = symbol.lock().unwrap(); + symbol.typed = shape.map(|(type_use, _)|type_use); + symbol.indirections = shape.map_or(0, |(_, layers)|layers); + symbol.typeof_declared = types.is_typeof_shape(typed); +} + +fn add_member_symbols(maker: &SymbolMaker, + types: &TypeDatabase, + owner: TypeUse, + members: Vec<&DMLTypeMember>, + anonymous: &mut HashMap>, + fields: &mut HashMap) { + for member in members { + if let Some(name) = &member.name { + fields.entry(name.span).or_insert_with(|| { + let member_symbol = symbol_ref!( + maker, + name.span, + DMLSymbolKind::Field, + SymbolSource::TypeMember(owner, name.span), + definitions = vec![name.span], + declarations = vec![name.span] + ); + set_symbol_type(&member_symbol, types, &member.typed); + member_symbol + }); + } + register_anonymous_types(maker, types, &member.typed, + anonymous, fields); + } +} + +// Creates field symbols for every anonymous struct-like type within 'typed' +fn register_anonymous_types(maker: &SymbolMaker, + types: &TypeDatabase, + typed: &DMLType, + anonymous: &mut HashMap>, + fields: &mut HashMap) { + if let Some(members) = direct_members(typed) { + if anonymous.contains_key(&typed.span) { + return; + } + anonymous.insert(typed.span, members.iter() + .filter_map(|member|member.name.clone()).collect()); + add_member_symbols(maker, types, TypeUse::Anonymous(typed.span), + members, anonymous, fields); + return; + } + match &typed.kind { + DMLTypeKind::Const(inner) | + DMLTypeKind::Pointer(inner) | + DMLTypeKind::Vector(inner) => + register_anonymous_types(maker, types, inner, anonymous, fields), + DMLTypeKind::Array { element, .. } => + register_anonymous_types(maker, types, element, anonymous, fields), + DMLTypeKind::Function { returns, parameters, .. } => { + register_anonymous_types(maker, types, returns, anonymous, fields); + for parameter in parameters { + register_anonymous_types(maker, types, parameter, + anonymous, fields); + } + }, + DMLTypeKind::Hook(arguments) => + for argument in arguments { + register_anonymous_types(maker, types, argument, + anonymous, fields); + }, + _ => (), + } +} + +fn populate_symbol_types(maker: &SymbolMaker, + storage: &mut SymbolStorage, + types: &TypeDatabase) { + let mut anonymous = HashMap::default(); + let mut fields = HashMap::default(); + for symbol in storage.all_symbols() { + let mut symbol = symbol.lock().unwrap(); + if symbol.typed.is_some() { + continue; + } + let type_syntax = match &symbol.source { + SymbolSource::DMLObject(DMLObject::ShallowObject(object)) => + match &object.variant { + DMLShallowObjectVariant::Session(variable) | + DMLShallowObjectVariant::Saved(variable) => + Some(&variable.declaration.type_syntax), + DMLShallowObjectVariant::Extern(external) => + Some(&external.get_likely_definition().var.typed), + // NOTE: Outside shared contexts the assigned value is + // known, so once constant propagation is in place member + // lookups should follow the value rather than the type + DMLShallowObjectVariant::Parameter(parameter) => + parameter.used_definitions.iter() + .chain(¶meter.definitions) + .chain(¶meter.declarations) + .find_map(|(_, declaration)| + declaration.typed.as_ref()), + _ => None, + }, + SymbolSource::MethodArg(method, name) => method.args().iter() + .find(|argument|argument.name().val == name.val) + .and_then(|argument|match argument { + DMLMethodArg::Typed(declaration) => + Some(&declaration.type_syntax), + DMLMethodArg::Inline(_) => None, + }), + _ => None, + }; + // Symbols without syntax here (e.g. locals) were typed on creation + let Some(type_syntax) = type_syntax else { + continue; + }; + let shape = types.resolve_type_shape(type_syntax); + let typeof_declared = types.is_typeof_shape(type_syntax); + register_anonymous_types(maker, types, type_syntax, + &mut anonymous, &mut fields); + symbol.typed = shape.map(|(type_use, _)|type_use); + symbol.indirections = shape.map_or(0, |(_, layers)|layers); + symbol.typeof_declared = typeof_declared; + } + for (span, members) in anonymous { + storage.anonymous_type_members.entry(span).or_insert(members); + } + for (span, field) in fields { + storage.type_member_symbols.entry(span).or_insert(field); + } +} + fn new_symbol_from_object(maker: &SymbolMaker, object: &DMLCompositeObject) -> SymbolRef { let all_decl_defs = &object.all_decls; @@ -1962,6 +2476,67 @@ fn new_symbol_from_object(maker: &SymbolMaker, bases = all_decl_defs.clone()) } +fn bind_interface_implementations(storage: &SymbolStorage, + objects: &StructureContainer, + types: &TypeDatabase) { + for object in objects.values() + .filter(|object|object.kind == CompObjectKind::Implement) { + let interface_name = format!("{}_interface_t", object.identity.val); + let type_ids = types.typedefs_named(&interface_name).iter() + .filter(|type_id|types.declaration(**type_id) + .is_some_and(|declaration|declaration.underlying.is_some())) + .copied().collect::>(); + let [type_id] = type_ids.as_slice() else { + continue; + }; + let Some(declaration) = types.declaration(*type_id) else { + continue; + }; + let Some(typedef_symbol) = storage.typedef_symbols.get(type_id) else { + continue; + }; + let Some(object_symbol) = storage.object_symbols.get(&object.key) else { + continue; + }; + + object_symbol.lock().unwrap().type_definitions + .insert(declaration.declaration_span); + { + let mut symbol = typedef_symbol.lock().unwrap(); + symbol.references.insert(object.declloc); + symbol.implementations.insert(object.declloc); + } + + let Some(underlying) = declaration.underlying.as_ref() else { + continue; + }; + for member in underlying.declaration_members() { + let Some(member_name) = &member.name else { + continue; + }; + let Some(method) = object.get_method(member_name.val.as_str()) else { + continue; + }; + let Some(method_symbol) = storage.method_symbols + .get(method.location()) + .and_then(|symbols|symbols.get(&object.key)) else { + continue; + }; + let Some(member_symbol) = storage.type_member_symbols + .get(&member_name.span) else { + continue; + }; + { + let mut symbol = method_symbol.lock().unwrap(); + symbol.bases = vec![member_name.span]; + symbol.declarations = vec![member_name.span]; + } + member_symbol.lock().unwrap().implementations + .insert(*method.location()); + } + } +} + fn new_symbol_from_arg(maker: &SymbolMaker, methref: &Arc, arg: &DMLMethodArg) -> SymbolRef { @@ -2007,6 +2582,7 @@ where K: std::hash::Hash + Eq + Clone + std::fmt::Debug, #[allow(clippy::ptr_arg)] fn add_new_symbol_from_shallow(maker: &SymbolMaker, shallow: &DMLShallowObject, + types: &TypeDatabase, errors: &mut Vec, storage: &mut SymbolStorage, method_structure: &mut HashMap @@ -2019,7 +2595,7 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, param.declarations.iter() .map(|(_, def)|*def.loc_span()).collect()), DMLShallowObjectVariant::Method(method_ref) => - return add_new_symbol_from_method(maker, &shallow.parent, method_ref, errors, storage, method_structure), + return add_new_symbol_from_method(maker, &shallow.parent, method_ref, types, errors, storage, method_structure), DMLShallowObjectVariant::Constant(constant) => (vec![*constant.loc_span()], vec![*constant.loc_span()], @@ -2080,6 +2656,7 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, fn add_method_scope_symbols(maker: &SymbolMaker, method: &Arc, + types: &TypeDatabase, method_structure: &mut HashMap, storage: &mut SymbolStorage, @@ -2093,10 +2670,24 @@ fn add_method_scope_symbols(maker: &SymbolMaker, add_new_method_scope_symbols(maker, method, &method.get_decl().body, + types, errors, storage, &mut entry); } + // Top-level locals share their block with the method arguments + for argument in method.args() { + if let Some(local) = entry.symbols.get(&argument.name().val) { + errors.push(DMLError { + span: local.lock().unwrap().loc, + description: format!("Duplicate definition of variable '{}'", + argument.name().val), + related: vec![(*argument.loc_span(), + "Previously defined here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } if !entry.is_empty() { method_structure.insert( *method.location(), @@ -2107,12 +2698,24 @@ fn add_method_scope_symbols(maker: &SymbolMaker, fn add_new_method_scope_symbol(maker: &SymbolMaker, method: &Arc, sym: &T, - _typ: &DMLType, + typ: &DMLType, + types: &TypeDatabase, + errors: &mut Vec, storage: &mut SymbolStorage, scope: &mut RangeEntry) where T : StructureSymbol + DMLNamed + LocationSpan { + if let Some(previous) = scope.symbols.get(&sym.name().val) { + errors.push(DMLError { + span: *sym.loc_span(), + description: format!("Duplicate definition of variable '{}'", + sym.name().val), + related: vec![(previous.lock().unwrap().loc, + "Previously defined here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } let symbol = symbol_ref!( maker, *sym.loc_span(), @@ -2120,8 +2723,11 @@ where SymbolSource::MethodLocal(Arc::clone(method), sym.name().clone()), definitions = vec![*sym.loc_span()], declarations = vec![*sym.loc_span()] - // TODO: resolve type ); + set_symbol_type(&symbol, types, typ); + register_anonymous_types(maker, types, typ, + &mut storage.anonymous_type_members, + &mut storage.type_member_symbols); scope.symbols.insert(sym.name().val.clone(), Arc::clone(&symbol)); storage.variable_symbols.insert(*sym.loc_span(), symbol); } @@ -2131,6 +2737,7 @@ fn enter_new_method_scope(maker: &SymbolMaker, outer_stmnt_desc: &'static str, stmnt: &Statement, scope_span: &ZeroSpan, + types: &TypeDatabase, errors: &mut Vec, storage: &mut SymbolStorage, scope: &mut RangeEntry) { @@ -2156,34 +2763,107 @@ fn enter_new_method_scope(maker: &SymbolMaker, add_new_method_scope_symbols(maker, method, stmnt, + types, errors, storage, &mut entry); scope.sub_ranges.push(entry); } +// 'each T in (...)' yields values of template type 'T'; other lists can be +// heterogeneous, so their elements are left untyped +fn iteration_variable_type(list: &Expression, fallback: ZeroSpan) -> DMLType { + match list.as_ref() { + ExpressionKind::EachIn(each) => DMLType { + kind: DMLTypeKind::Named(each.templ.clone()), + span: each.templ.span, + }, + _ => DMLType::unknown(fallback), + } +} + +fn add_switch_case_symbols(maker: &SymbolMaker, + method: &Arc, + cases: &[SwitchCase], + types: &TypeDatabase, + errors: &mut Vec, + storage: &mut SymbolStorage, + scope: &mut RangeEntry) { + for case in cases { + match case { + SwitchCase::Statement(statement) => + if matches!(statement.as_ref(), StatementKind::Compound(_)) { + enter_new_method_scope(maker, method, "block", statement, + statement.span(), types, errors, + storage, scope); + } else { + add_new_method_scope_symbols(maker, method, statement, + types, errors, storage, scope); + }, + // Each '#if' branch is a block of its own + SwitchCase::HashIf(hashif) => + for branch in [&hashif.truecases, &hashif.falsecases] { + let Some(range) = branch.iter() + .map(|case|case.span().range) + .reduce(ZeroRange::combine) else { + continue; + }; + let mut entry = RangeEntry { + range, + symbols: HashMap::default(), + sub_ranges: vec![], + }; + add_switch_case_symbols(maker, method, branch, types, + errors, storage, &mut entry); + scope.sub_ranges.push(entry); + }, + SwitchCase::Case(_) | SwitchCase::Default(_) => (), + } + } +} + fn add_new_method_scope_symbols(maker: &SymbolMaker, method: &Arc, stmnt: &Statement, + types: &TypeDatabase, errors: &mut Vec, storage: &mut SymbolStorage, scope: &mut RangeEntry) { match &**stmnt { StatementKind::Compound(content) => for sub_stmnt in &content.statements { - add_new_method_scope_symbols(maker, - method, - sub_stmnt, - errors, - storage, - scope); + if matches!(sub_stmnt.as_ref(), StatementKind::Compound(_)) { + enter_new_method_scope(maker, + method, + "block", + sub_stmnt, + sub_stmnt.span(), + types, + errors, + storage, + scope); + } else { + add_new_method_scope_symbols(maker, + method, + sub_stmnt, + types, + errors, + storage, + scope); + } }, + // DMLC declares locals of a switch body in the enclosing block, so + // they remain visible after the switch + StatementKind::Switch(content) => + add_switch_case_symbols(maker, method, &content.cases, types, + errors, storage, scope), StatementKind::If(content) => { enter_new_method_scope(maker, method, "if", &content.ifbody, content.ifbody.span(), + types, errors, storage, scope); @@ -2193,6 +2873,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "else", elsebody, elsebody.span(), + types, errors, storage, scope); @@ -2205,6 +2886,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "#if", &content.ifbody, content.ifbody.span(), + types, errors, storage, scope); @@ -2214,6 +2896,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "#else", elsebody, elsebody.span(), + types, errors, storage, scope); @@ -2225,6 +2908,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "while", &content.body, content.body.span(), + types, errors, storage, scope), @@ -2234,6 +2918,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "do-while", &content.body, content.body.span(), + types, errors, storage, scope), @@ -2250,6 +2935,8 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, method, decl, &decl.typed, + types, + errors, storage, &mut entry); } @@ -2259,6 +2946,34 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "for", &content.body, content.body.span(), + types, + errors, + storage, + &mut entry); + scope.sub_ranges.push(entry); + }, + StatementKind::ForEach(content) => { + let mut entry = RangeEntry { + range: content.span().range, + symbols: HashMap::default(), + sub_ranges: vec![], + }; + let identifier_type = iteration_variable_type( + &content.inexpr, *content.identifier.loc_span()); + add_new_method_scope_symbol(maker, + method, + &content.identifier, + &identifier_type, + types, + errors, + storage, + &mut entry); + enter_new_method_scope(maker, + method, + "foreach", + &content.body, + content.body.span(), + types, errors, storage, &mut entry); @@ -2272,11 +2987,14 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, symbols: HashMap::default(), sub_ranges: vec![], }; + let identifier_type = iteration_variable_type( + &content.inexpr, *content.ident.loc_span()); add_new_method_scope_symbol(maker, method, &content.ident, - // TODO: infer type - content.ident.loc_span(), + &identifier_type, + types, + errors, storage, &mut entry); enter_new_method_scope(maker, @@ -2284,6 +3002,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "select", &content.selectbranch, content.selectbranch.span(), + types, errors, storage, &mut entry); @@ -2293,6 +3012,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "select-else", &content.elsebranch, content.elsebranch.span(), + types, errors, storage, scope); @@ -2303,6 +3023,7 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "try", &content.tryblock, content.tryblock.span(), + types, errors, storage, scope); @@ -2311,10 +3032,33 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, "catch", &content.catchblock, content.catchblock.span(), + types, errors, storage, scope); }, + // Message component names are only in scope in the call + StatementKind::After(content) => { + let mut entry = RangeEntry { + range: content.call.span().range, + symbols: HashMap::default(), + sub_ranges: vec![], + }; + for component in content.message_components() { + let unknown_type = DMLType::unknown(*component.loc_span()); + add_new_method_scope_symbol(maker, + method, + component, + &unknown_type, + types, + errors, + storage, + &mut entry); + } + if !entry.symbols.is_empty() { + scope.sub_ranges.push(entry); + } + }, StatementKind::VariableDecl(content) => for decl in &content.vars { add_new_method_scope_symbol( @@ -2322,6 +3066,8 @@ fn add_new_method_scope_symbols(maker: &SymbolMaker, method, decl, &decl.typed, + types, + errors, storage, scope); }, @@ -2338,6 +3084,7 @@ impl DeviceAnalysis { files: &HashMap<&str, &TopLevel>, imp_map: &HashMap, eval_context: &mut EvaluationContext, + types: &TypeDatabase, errors: &mut Vec) -> TemplateTraitInfo { info!("Rank templates"); @@ -2348,7 +3095,7 @@ impl DeviceAnalysis { create_templates_traits( start_of_file, rank_maker, templates, order, - invalid_isimps, imp_map, rank_struct, eval_context, errors) + invalid_isimps, imp_map, rank_struct, eval_context, types, errors) } fn match_references(&mut self, @@ -2360,6 +3107,7 @@ impl DeviceAnalysis { let reference_cache: Mutex = Mutex::new( ReferenceCache::new(self.reference_cache_max_size) ); + let resolved_method_calls = Mutex::new(HashMap::new()); for scope_chain in all_scopes(bases) { debug!("Got scope at {:?}", scope_chain.last() .map(|s|s.span().start_position())); @@ -2367,8 +3115,22 @@ impl DeviceAnalysis { errors, method_structure, &reference_cache, + &resolved_method_calls, status)?; } + for ((object, reference), methods) in + resolved_method_calls.into_inner().unwrap() + { + let Some(method) = methods.first() else { continue; }; + if methods.iter().all(|candidate| + candidate.location() == method.location()) + { + if let Some(object) = self.objects.get_mut(object) { + object.resolved_method_calls.insert( + reference, Arc::clone(method)); + } + } + } Ok(()) } @@ -2434,7 +3196,9 @@ impl DeviceAnalysis { } } - let base_templates = templates_from_device_and_bases(&root, &bases); + let mut base_templates = templates_from_device_and_bases(&root, &bases); + // Bases arrive in arbitrary order; earliest span wins among duplicates + base_templates.sort_by_key(|template|*template.obj.object.loc_span()); trace!("base template names: {:?}", base_templates.iter() .fold("".to_string(), @@ -2471,6 +3235,49 @@ impl DeviceAnalysis { } } + let mut types = TypeDatabase::default(); + report_global_name_collisions(&bases, &mut errors); + let loggroups = collect_loggroups(&bases, &mut errors); + for base in &bases { + for typedef in &base.toplevel.typedefs { + let name = typedef.object.name.val.as_str(); + if let Some(previous_span) = types.typedefs_named(name).first() + .and_then(|id|types.declaration(*id)) + .map(|decl|decl.name.span) { + errors.push(DMLError { + span: typedef.object.name.span, + description: format!("Duplicate typedef name; '{}'", name), + severity: Some(DiagnosticSeverity::ERROR), + related: vec![(previous_span, + "Previously defined here".to_string())], + }); + } + if let Some(template) = unique_templates.get(name) { + errors.push(DMLError { + span: typedef.object.name.span, + description: format!( + "Typedef name '{}' collides with a template name", + name), + severity: Some(DiagnosticSeverity::ERROR), + related: vec![(*template.obj.object.loc_span(), + "Template defined here".to_string())], + }); + } + types.add_typedef(typedef); + } + } + for template in unique_templates.values() { + types.add_template(template.obj.object.name.clone()); + } + for base in &bases { + for typedef in &base.toplevel.typedefs { + types.report_unknown_types(&typedef.typed, &mut errors); + } + report_unknown_declared_types( + &base.toplevel.spec, &types, &mut EvaluationContext::new(), + &mut errors); + } + // Add files-as-templates let mut files: HashMap<&str, &TopLevel> = HashMap::default(); for base in &bases { @@ -2488,6 +3295,7 @@ impl DeviceAnalysis { &files, &imp_map, &mut eval_context, + &types, &mut errors); status.check_alive()?; // TODO: catch typedef/traitname overlaps @@ -2497,7 +3305,7 @@ impl DeviceAnalysis { info!("Make device"); let device_key = make_device(&root.path, &root.toplevel, &tt_info, imp_map, &mut container, - &mut rank_maker, &mut eval_context, + &mut rank_maker, &mut eval_context, &types, &mut errors).key; status.check_alive()?; // maps template declaration loc to objects @@ -2515,6 +3323,7 @@ impl DeviceAnalysis { let maker = SymbolMaker::new(); let mut symbol_info = objects_to_symbols(&maker, &container, + &types, &mut errors, &mut method_structure); // This needs to be done after all symbols are created, because method @@ -2522,18 +3331,24 @@ impl DeviceAnalysis { bind_method_implementations(&mut symbol_info.method_symbols); status.check_alive()?; - // TODO: how do we store type info? - extend_with_templates(&maker, &mut symbol_info, &tt_info); - //extend_with_types(&mut symbols, ??) + + extend_with_templates(&maker, &mut symbol_info, &tt_info, &types); + extend_with_types(&maker, &mut symbol_info, &types); + extend_with_loggroups(&maker, &mut symbol_info, &bases); + populate_symbol_types(&maker, &mut symbol_info, &types); + bind_interface_implementations(&symbol_info, &container, &types); let mut device = DeviceAnalysis { name: root.toplevel.device.unwrap().name.val, errors: HashMap::default(), objects: container, device_obj: DMLObject::CompObject(device_key), templates: tt_info, + types, + loggroups, symbol_info, reference_info: ReferenceStorage::default(), template_object_implementation_map, + conditional_declarations: conditional_declarations(&bases), path: root.path.clone(), clientpath: root.path.clone().into(), dependant_files: bases.iter().map(|b|&b.path).cloned().collect(), @@ -2544,6 +3359,8 @@ impl DeviceAnalysis { &method_structure, &mut errors, &status)?; + check_method_body_types_for_objects( + &device.objects, &device.types, &mut errors); // NOTE: This is when we previously pre-calculated the ref->symbol map, // however the up-front analysis cost of this was too heavy @@ -2552,9 +3369,15 @@ impl DeviceAnalysis { info!("Invariant check"); for obj in device.objects.values() { device.param_invariants(obj, &mut errors); + device.report_subobject_name_collisions(obj, &mut errors); } - for error in errors { + // Object-level checks repeat for every instance of a template + let mut seen = HashSet::new(); + for error in drop_repeated_collisions(errors) { + if !seen.insert(error.clone()) { + continue; + } device.errors.entry(error.span.path()) .or_default() .push(error); @@ -2565,6 +3388,49 @@ impl DeviceAnalysis { Ok(device) } + // A 'name' parameter can make subobjects collide (DMLC ENAMECOLL) + fn report_subobject_name_collisions(&self, + obj: &DMLCompositeObject, + report: &mut Vec) { + let mut children: Vec<_> = obj.components.iter() + .filter_map(|(identity, component)| + component.resolve(&self.objects).as_comp() + .map(|child|(identity, child))) + .collect(); + children.sort_by_key(|(identity, _)|identity.as_str()); + let mut name_sites: HashMap = HashMap::default(); + for (identity, child) in children { + let renamed = child.get_param("name") + .and_then(|param|param.get_unambiguous_def()) + .and_then(|def|match def.value.as_ref() { + Some(ParamValue::Set(expression)) => + match expression.as_ref() { + ExpressionKind::StringLiteral(name) => + Some((name.val.clone(), name.span)), + _ => None, + }, + _ => None, + }); + let (name, site) = renamed.unwrap_or_else( + ||(identity.clone(), child.declloc)); + let previous = name_sites.get(&name) + .map(|site|(*site, "Previously defined here")) + .or_else(||(name == "dev" && obj.kind == CompObjectKind::Device) + .then_some((obj.declloc, + "The device itself is logged as 'dev'"))); + if let Some((previous, label)) = previous { + report.push(DMLError { + span: site, + description: format!("Name collision on '{}'", name), + related: vec![(previous, label.to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } else { + name_sites.insert(name, site); + } + } + } + #[allow(clippy::ptr_arg)] fn param_invariants(&self, obj: &DMLCompositeObject, @@ -2614,6 +3480,147 @@ impl DeviceAnalysis { locked_info.get(&loc).unwrap().clone() } } + +} + +// Checks data, hook, extern and typed parameter declarations, including those +// in templates that are never instantiated +// Export names share a device-global namespace (DMLC ENAMECOLL) +fn report_global_name_collisions(bases: &[IsolatedAnalysis], + errors: &mut Vec) { + fn report(kind: &str, + mut names: Vec<(DMLString, ExistCondition)>, + errors: &mut Vec) { + names.sort_by_key(|(name, _)|name.span); + for (index, (name, cond)) in names.iter().enumerate() { + if let Some((previous, _)) = names[..index].iter().find( + |(other, other_cond)|other.val == name.val + && !cond.guaranteed_excluded_from(other_cond)) { + errors.push(DMLError { + span: name.span, + description: format!("Duplicate {} name; '{}'", + kind, name.val), + severity: Some(DiagnosticSeverity::ERROR), + related: vec![(previous.span, + "Previously defined here".to_string())], + }); + } + } + } + report("export", bases.iter() + .flat_map(|base|&base.toplevel.spec.exports) + .filter_map(|export|match export.obj.name.as_ref() { + ExpressionKind::StringLiteral(name) => Some((DMLString { + val: name.val.clone(), + span: name.span, + }, export.cond.clone())), + _ => None, + }) + .collect(), errors); +} + +fn report_unknown_declared_types(spec: &StatementSpec, + types: &TypeDatabase, + context: &mut EvaluationContext, + errors: &mut Vec) { + for external in &spec.externs { + types.report_unknown_types(&external.var.typed, errors); + } + for variable in spec.sessions.iter().chain(&spec.saveds) { + if variable.cond.exists_no_report(context) { + for declaration in &variable.obj.vars { + types.report_unknown_types(&declaration.typed, errors); + } + } + } + for hook in &spec.hooks { + if hook.cond.exists_no_report(context) { + for argument in &hook.obj.args { + types.report_unknown_types(argument, errors); + } + } + } + for param in &spec.params { + if let Some(typed) = param.obj.typed.as_ref() + .filter(|_|param.cond.exists_no_report(context)) { + types.report_unknown_types(typed, errors); + } + } + for object in &spec.objects { + if object.cond.exists_no_report(context) { + report_unknown_declared_types(&object.spec, types, context, errors); + } + } + for nested in spec.ineachs.iter().map(|ineach|(&ineach.cond, &ineach.spec)) + .chain(spec.templates.iter() + .map(|template|(&template.cond, &template.spec))) { + if nested.0.exists_no_report(context) { + report_unknown_declared_types(nested.1, types, context, errors); + } + } +} + +fn conditional_declarations(bases: &[IsolatedAnalysis]) -> HashSet { + fn gather(spec: &StatementSpec, found: &mut HashSet) { + for object in &spec.objects { + if !matches!(object.cond, ExistCondition::Always) { + found.insert(*object.loc_span()); + } + gather(&object.spec, found); + } + for method in &spec.methods { + if !matches!(method.cond, ExistCondition::Always) { + found.insert(*method.loc_span()); + } + } + for ineach in &spec.ineachs { + gather(&ineach.spec, found); + } + for template in &spec.templates { + gather(&template.spec, found); + } + } + let mut found = HashSet::default(); + for base in bases { + gather(&base.toplevel.spec, &mut found); + } + found +} + +// The same name clash can be found both when processing a template and when +// constructing an object, keep one report per set of clashing declarations +fn drop_repeated_collisions(errors: Vec) -> Vec { + fn clashing_declarations(error: &DMLError) -> Option> { + if !error.description.starts_with("Name collision") { + return None; + } + let mut spans: Vec = std::iter::once(error.span) + .chain(error.related.iter().map(|(span, _)|*span)).collect(); + spans.sort(); + spans.dedup(); + Some(spans) + } + let mut reported: Vec> = vec![]; + let mut kept = vec![]; + let mut collisions = vec![]; + for error in errors { + match clashing_declarations(&error) { + Some(spans) => collisions.push((spans, error)), + None => kept.push(error), + } + } + // Widest reports first, so narrower repeats are recognized as covered + collisions.sort_by_key(|(spans, _)|std::cmp::Reverse(spans.len())); + for (spans, error) in collisions { + if reported.iter().any(|covered|spans.iter().all( + |span|covered.contains(span))) { + continue; + } + reported.push(spans); + kept.push(error); + } + kept + } pub fn templates_from_device_and_bases<'a>(_device: &'a IsolatedAnalysis, @@ -2668,7 +3675,9 @@ fn bind_method_implementations(method_symbols: &mut HashMap, errors: &mut Vec, storage: &mut SymbolStorage, method_structure: &mut HashMap) { +fn add_new_symbol_from_method(maker: &SymbolMaker, parent_obj_key: &StructureKey, method_ref: &Arc, + types: &TypeDatabase, + errors: &mut Vec, storage: &mut SymbolStorage, method_structure: &mut HashMap) { let (bases, definitions, declarations) = ( method_ref.get_bases().iter().map(|b|*b.location()).collect(), vec![*method_ref.get_decl().location()], @@ -2690,10 +3699,10 @@ fn add_new_symbol_from_method(maker: &SymbolMaker, parent_obj_key: &StructureKey let new_argsymbol = new_symbol_from_arg(maker, method_ref, arg); log_non_same_insert(&mut storage.variable_symbols, *arg.loc_span(), new_argsymbol); } - add_method_scope_symbols(maker, method_ref, method_structure, storage, errors); + add_method_scope_symbols(maker, method_ref, types, method_structure, storage, errors); if let Some(defaults) = method_ref.get_default() { for default in defaults.flat_refs() { - add_new_symbol_from_method(maker, parent_obj_key, default, errors, storage, method_structure); + add_new_symbol_from_method(maker, parent_obj_key, default, types, errors, storage, method_structure); } } } @@ -2702,11 +3711,32 @@ fn add_new_symbol_from_method(maker: &SymbolMaker, parent_obj_key: &StructureKey #[cfg(test)] mod symbol_storage_tests { use super::*; + use crate::analysis::structure::objects::{ + DMLObjectCommon, Extern, VariableDecl, VariableDeclKind, + }; + use crate::analysis::templating::objects::DMLAmbiguousDef; fn symbol(maker: &SymbolMaker) -> SymbolRef { let location = ZeroSpan::invalid("symbol-storage-test.dml"); + let name = "dummy_extern".to_string(); + let extrn = Extern { + var: VariableDecl { + object: DMLObjectCommon { + name: DMLString { val: name.clone(), span: location }, + span: location, + }, + kind: VariableDeclKind::Extern, + typed: DMLType::unknown(location), + }, + span: location, + }; + let object = DMLObject::ShallowObject(Box::new(DMLShallowObject { + parent: StructureKey::default(), + variant: DMLShallowObjectVariant::Extern( + DMLAmbiguousDef::new_simple(name, extrn)), + })); maker.new_symbol(location, DMLSymbolKind::Extern, - SymbolSource::Type(DMLResolvedType::Dummy(location))) + SymbolSource::DMLObject(object)) } #[test] diff --git a/src/analysis/parsing/expression.rs b/src/analysis/parsing/expression.rs index 14795c5e..09909fd5 100644 --- a/src/analysis/parsing/expression.rs +++ b/src/analysis/parsing/expression.rs @@ -13,7 +13,7 @@ use crate::analysis::parsing::misc::{objident_filter, SingleInitializer}; use crate::analysis::reference::{CodeReference, Reference, ReferenceKind, MaybeIsNodeRef, NodeRef}; use crate::analysis::FileSpec; -use crate::analysis::structure::expressions::DMLString; +use crate::analysis::structure::expressions::{DMLString, MemberOp}; use crate::analysis::{DeclarationSpan, LocalDMLError}; use crate::vfs::TextFile; @@ -126,6 +126,10 @@ impl TreeElement for MemberLiteralContent { if let Some(refr) = self.maybe_noderef(file) { accumulator.push(CodeReference::from_noderef( refr, ReferenceKind::Variable).into()); + } else if self.left.maybe_noderef(file).is_none() { + if let Some(member) = DMLString::from_token(&self.right, file) { + accumulator.push(Reference::UntrackedMember(member)); + } } } } @@ -133,12 +137,19 @@ impl TreeElement for MemberLiteralContent { impl MaybeIsNodeRef for MemberLiteralContent { fn maybe_noderef<'a>(&self, file: FileSpec<'a>) -> Option { let left_noderef = self.left.maybe_noderef(file)?; + let op = if self.operation.get_token() + .is_some_and(|token|token.kind == TokenKind::Arrow) { + MemberOp::Arrow + } else { + MemberOp::Dot + }; let right_node = DMLString::from_token(&self.right, file)?; let span = ZeroSpan::combine( *left_noderef.span(), *right_node.span()); Some(NodeRef::Sub( Box::new(left_noderef), + op, right_node, span)) } @@ -458,6 +469,7 @@ impl TreeElement for IndexContent { impl MaybeIsNodeRef for IndexContent { fn maybe_noderef<'a>(&self, file: FileSpec<'a>) -> Option { self.array.maybe_noderef(file) + .map(|array|NodeRef::Index(Box::new(array))) } } diff --git a/src/analysis/parsing/misc.rs b/src/analysis/parsing/misc.rs index 382f8bc0..b48fbc02 100644 --- a/src/analysis/parsing/misc.rs +++ b/src/analysis/parsing/misc.rs @@ -648,6 +648,7 @@ impl CDecl { pub fn ident_filter(token: TokenKind) -> bool { matches!(token, TokenKind::Identifier | TokenKind::Attribute | TokenKind::Bank | + TokenKind::Bitorder | TokenKind::Provisional | TokenKind::Then | TokenKind::Connect | TokenKind::Constant | TokenKind::Data | TokenKind::Device | TokenKind:: Event | TokenKind::Field | TokenKind::Footer | TokenKind::Group | TokenKind::Header | diff --git a/src/analysis/parsing/statement.rs b/src/analysis/parsing/statement.rs index 52b2c939..e824950a 100644 --- a/src/analysis/parsing/statement.rs +++ b/src/analysis/parsing/statement.rs @@ -1365,8 +1365,8 @@ impl Parse for AfterContent { let mut cont = true; while cont { cont = false; - let bind = inner.expect_next_kind( - stream, TokenKind::Identifier); + let bind = inner.expect_next_filter( + stream, ident_filter, "identifier"); let comma = if inner.peek_kind(stream) == Some( TokenKind::Comma) { cont = true; @@ -1389,8 +1389,8 @@ impl Parse for AfterContent { leaf => { // Sanity, the rightside of hook must be an identifier // There is no good way to reconstruct parser state here - if leaf.get_token().map(|l|l.kind) - != Some(TokenKind::Identifier) { + if !leaf.get_token().is_some_and( + |l|ident_filter(l.kind)) { error!("Internal Parser Error: Unexpectedly \ got '-> {:?}'", leaf); } diff --git a/src/analysis/parsing/structure.rs b/src/analysis/parsing/structure.rs index 724a94f6..2afbff4c 100644 --- a/src/analysis/parsing/structure.rs +++ b/src/analysis/parsing/structure.rs @@ -193,7 +193,7 @@ impl TreeElement for MethodContent { } if self.memoized.is_some() { errors.push(LocalDMLError { - range: self.startup.range(), + range: self.memoized.range(), description: "a method declared as memoized \ must also be declared \ independent".to_string(), @@ -203,7 +203,7 @@ impl TreeElement for MethodContent { if self.memoized.is_some() && self.startup.is_none() { errors.push(LocalDMLError { - range: self.startup.range(), + range: self.memoized.range(), description: "a method declared as memoized \ must also be declared \ startup".to_string(), @@ -981,8 +981,8 @@ fn parse_composite_object_name(context: &ParseContext, understands_ellipsis); let mut lessthan_context = ellipsis_context.enter_context( understands_lessthan); - let indexvar = lessthan_context.expect_next_kind( - stream, TokenKind::Identifier); + let indexvar = lessthan_context.expect_next_filter( + stream, ident_filter, "identifier"); let lessthan = ellipsis_context.expect_next_kind( stream, TokenKind::LessThan); let size = match bracket_context.peek_kind(stream) { @@ -1364,8 +1364,8 @@ fn bitorder_parse(context: &ParseContext, -> BitorderContent { let (mut outer, mut inner) = object_contexts(context); let bitorder = inner.expect_next_kind(stream, TokenKind::Bitorder); - let bitorderdesc = inner.expect_next_kind( - stream, TokenKind::Identifier); + let bitorderdesc = inner.expect_next_filter( + stream, ident_filter, "identifier"); let semi = outer.expect_next_kind(stream, TokenKind::SemiColon); BitorderContent { bitorder, bitorderdesc, semi @@ -1599,8 +1599,8 @@ fn parse_hook(shared: Option, let hook = outer.next_leaf(stream); let lparen = outer.expect_next_kind(stream, TokenKind::LParen); let arg_types = CDeclList::parse(context, stream, file_info); - let rparen = outer.expect_next_kind(stream, TokenKind::RParen); - let ident = outer.expect_next_kind(stream, TokenKind::Identifier); + let rparen = outer.expect_next_kind(stream, TokenKind::RParen); + let ident = outer.expect_next_filter(stream, ident_filter, "identifier"); let semi = outer.expect_next_kind(stream, TokenKind::SemiColon); DMLObjectContent::Hook(HookContent { shared, @@ -1635,7 +1635,7 @@ impl Parse for LoggroupContent { -> DMLObject { let (mut outer, mut inner) = object_contexts(context); let loggroup = inner.expect_next_kind(stream, TokenKind::Loggroup); - let ident = inner.expect_next_kind(stream, TokenKind::Identifier); + let ident = inner.expect_next_filter(stream, ident_filter, "identifier"); let semi = outer.expect_next_kind(stream, TokenKind::SemiColon); DMLObjectContent::Loggroup(LoggroupContent { loggroup, ident, semi @@ -1837,7 +1837,8 @@ fn device_parse(context: &ParseContext, _file_info: &FileInfo) -> DeviceContent { let (mut outer, mut inner) = object_contexts(context); let device = inner.expect_next_kind(stream, TokenKind::Device); - let name = inner.expect_next_kind(stream, TokenKind::Identifier); + let name = inner.expect_next_filter( + stream, objident_filter, "device name"); let semi = outer.expect_next_kind(stream, TokenKind::SemiColon); DeviceContent { device, name, semi @@ -2202,7 +2203,8 @@ fn provisionals_parse(context: &ParseContext, let mut activations = vec![]; while cont { cont = false; - let activate = inner.expect_next_kind(stream, TokenKind::Identifier); + let activate = inner.expect_next_filter( + stream, ident_filter, "identifier"); let comma = if inner.peek_kind(stream) == Some(TokenKind::Comma) { cont = true; Some(inner.next_leaf(stream)) @@ -2501,4 +2503,49 @@ mod test { &expected, &vec![]); } + + #[test] + fn test_device_keyword_name() { + let expected = make_ast( + zero_range(0, 0, 0, 16), + DMLObjectContent::Device(DeviceContent { + device: make_leaf(zero_range(0, 0, 0, 0), + zero_range(0, 0, 0, 6), + TokenKind::Device), + name: make_leaf(zero_range(0, 0, 6, 7), + zero_range(0, 0, 7, 15), + TokenKind::Constant), + semi: make_leaf(zero_range(0, 0, 15, 15), + zero_range(0, 0, 15, 16), + TokenKind::SemiColon), + })); + test_ast_tree::( + "device constant;", + &expected, + &vec![]); + } + + #[test] + fn test_keyword_idents() { + use logos::Logos; + for source in ["hook() constant;", + "shared hook(int) param;", + "loggroup device;", + "provisional constant, param;", + "bitorder then;", + "group g[param < 2][provisional < 3] {}", + "method m() { after h -> (constant, param): x(); }", + "method m() { after h -> constant: x(); }"] { + let lexer = TokenKind::lexer(source); + let mut stream = FileParser::new(lexer); + let context = ParseContext::new_context(doesnt_understand_tokens); + let obj = DMLObject::parse(&context, &mut stream, + &FileInfo::default()); + assert!(stream.skipped_tokens.is_empty(), + "{}: {:?}", source, stream.skipped_tokens); + assert!(obj.report_missing().is_empty(), + "{}: {:?}", source, obj.report_missing()); + assert!(stream.peek().is_none(), "{}", source); + } + } } diff --git a/src/analysis/parsing/types.rs b/src/analysis/parsing/types.rs index e9745d2f..ed9a95fe 100644 --- a/src/analysis/parsing/types.rs +++ b/src/analysis/parsing/types.rs @@ -29,6 +29,31 @@ pub fn typeident_filter(token: TokenKind) -> bool { } } +// Struct-like member names must be present and unique (DMLC ENAMECOLL) +fn check_member_names<'a>(members: impl Iterator, + file: &TextFile) -> Vec { + let mut used_names = vec![]; + let mut errors = vec![]; + for member in members { + match member.get_name() { + None => errors.append(&mut member.ensure_named()), + Some(token) => { + let name = token.read_token(file); + if used_names.contains(&name) { + errors.push(LocalDMLError { + range: member.range(), + description: "The name is already in use in the \ + same scope".to_string(), + }); + } else { + used_names.push(name); + } + }, + } + } + errors +} + #[derive(PartialEq, Clone, Debug)] pub struct StructTypeContent { pub structtok: LeafToken, @@ -47,12 +72,8 @@ impl TreeElement for StructTypeContent { &self.members, &self.rbrace) } - fn post_parse_sanity(&self, _file: &TextFile) -> Vec { - let mut errors = vec![]; - for (decl, _) in &self.members { - errors.append(&mut decl.ensure_named()); - } - errors + fn post_parse_sanity(&self, file: &TextFile) -> Vec { + check_member_names(self.members.iter().map(|(decl, _)|decl), file) } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.indent_code_block.check(IndentCodeBlockArgs::from_struct_type_content(self, aux.depth), acc); @@ -121,10 +142,8 @@ impl TreeElement for LayoutContent { &self.rbrace) } fn post_parse_sanity(&self, file: &TextFile) -> Vec { - let mut errors = vec![]; - for (field, _) in &self.fields { - errors.append(&mut field.ensure_named()); - } + let mut errors = check_member_names( + self.fields.iter().map(|(field, _)|field), file); if let Some(byteorder) = self.byteorder.read_leaf(file) { if byteorder != r#""big-endian""# && byteorder != r#""little-endian""# { @@ -161,16 +180,14 @@ impl Parse for LayoutContent { stream, TokenKind::StringConstant); let lbrace = new_context.expect_next_kind(stream, TokenKind::LBrace); let mut fields = vec![]; - while match new_context.peek_kind(stream) { - Some(TokenKind::RBrace) | None => false, - Some(_) => { - let field = CDecl::parse(&list_context, stream, file_info); - let semi = list_context.expect_next_kind( - stream, TokenKind::SemiColon); - fields.push((field, semi)); - true - }, - } {} + let mut cont = !new_context.peek_kind(stream).is_none_or(|t|t == TokenKind::RBrace); + while cont { + let field = CDecl::parse(&list_context, stream, file_info); + let semi = list_context.expect_next_kind( + stream, TokenKind::SemiColon); + fields.push((field, semi)); + cont = new_context.peek_kind(stream).is_some_and(|t|CDecl::first_token_matcher(t) || t == TokenKind::SemiColon); + } let rbrace = new_context.expect_next_kind(stream, TokenKind::RBrace); BaseTypeContent::Layout(LayoutContent { layout, @@ -290,28 +307,18 @@ impl TreeElement for BitfieldsContent { &self.rbrace) } fn post_parse_sanity(&self, file: &TextFile) -> Vec { - let mut used_names = vec![]; let mut errors = vec![]; - for field in &self.fields { - match field.cdecl.get_name() { - None => errors.push(LocalDMLError { - range: field.cdecl.range(), - description: "missing name in declaration".to_string(), - }), - Some(tok) => { - let name = tok.read_token(file); - if used_names.contains(&name) { - errors.push(LocalDMLError { - range: field.cdecl.range(), - description: "The name is already in use in the \ - same scope".to_string(), - }) - } else { - used_names.push(name) - } - } - } + if self.iconst.read_leaf(file) + .map(|width|width.replace('_', "").parse::()) + .is_some_and(|width|!matches!(width, Ok(1..=64))) { + errors.push(LocalDMLError { + range: self.iconst.range(), + description: "Bitfields width must be an integer between 1 and 64" + .to_string(), + }); } + errors.append(&mut check_member_names( + self.fields.iter().map(|field|&field.cdecl), file)); errors } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { @@ -610,6 +617,66 @@ impl TreeElement for CTypeDeclSimpleContent { pub type CTypeDeclSimple = AstObject; +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use logos::Logos; + + use super::*; + + fn bitfields_sanity_errors(width: &str) -> Vec { + sanity_errors(&format!("bitfields {width} {{}}")) + } + + fn sanity_errors(source: &str) -> Vec { + let mut parser = FileParser::new(TokenKind::lexer(source)); + let context = ParseContext::new_context(doesnt_understand_tokens); + let ast = BaseType::parse(&context, &mut parser, &FileInfo::default()); + assert!(parser.skipped_tokens.is_empty(), + "unexpected skipped tokens: {:?}", parser.skipped_tokens); + + let file = TextFile::from_str(source).unwrap(); + let mut errors = vec![]; + ast.post_parse_sanity_walk(&mut errors, &file); + errors + } + + #[test] + fn struct_like_member_names_must_be_unique() { + for source in ["struct { int i; int i; }", + "layout \"big-endian\" { uint32 i; uint32 i; }", + "bitfields 32 { uint16 i @ [15:0]; uint16 i @ [31:16]; }", + "struct { struct { int j; int j; } inner; }"] { + let errors = sanity_errors(source); + assert_eq!(errors.len(), 1, "{}: {:?}", source, errors); + assert_eq!(errors[0].description, + "The name is already in use in the same scope"); + } + for source in ["struct { int i; struct { int i; } inner; }", + "layout \"big-endian\" { uint32 i; uint32 j; }"] { + assert!(sanity_errors(source).is_empty(), "{}", source); + } + } + + #[test] + fn bitfields_width_must_be_valid_integer_width() { + for width in ["1", "64", "6_4"] { + assert!(bitfields_sanity_errors(width).is_empty(), + "width {} should be valid", width); + } + + for width in ["0", "65", "999999999999999999999999999999"] { + let errors = bitfields_sanity_errors(width); + assert_eq!(errors.len(), 1, "width {} should be invalid", width); + assert_eq!(errors[0].description, + "Bitfields width must be an integer between 1 and 64"); + assert_eq!(errors[0].range, + ZeroRange::from_u32(0, 0, 10, 10 + width.len() as u32)); + } + } +} + impl Parse for CTypeDeclSimple { fn parse(context: &ParseContext, stream: &mut FileParser<'_>, file_info: &FileInfo) diff --git a/src/analysis/reference.rs b/src/analysis/reference.rs index 6aafc162..7b34d761 100644 --- a/src/analysis/reference.rs +++ b/src/analysis/reference.rs @@ -2,14 +2,27 @@ // SPDX-License-Identifier: Apache-2.0 and MIT use crate::analysis::{DeclarationSpan, LocationSpan, DMLNamed, ZeroSpan}; use crate::analysis::parsing::tree::{LeafToken, TreeElement}; -use crate::analysis::structure::expressions::{DMLString}; +use crate::analysis::structure::expressions::{DMLString, MemberOp}; use crate::analysis::FileSpec; #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum NodeRef { Simple(DMLString), - //// Ignore index here - Sub(Box, DMLString, ZeroSpan), + Sub(Box, MemberOp, DMLString, ZeroSpan), + Index(Box), +} + +impl NodeRef { + // The innermost non-indexed reference, and how many times it is indexed + pub fn strip_indices(&self) -> (&NodeRef, u32) { + match self { + NodeRef::Index(inner) => { + let (base, indices) = inner.strip_indices(); + (base, indices + 1) + }, + _ => (self, 0), + } + } } impl std::fmt::Display for NodeRef { @@ -17,10 +30,11 @@ impl std::fmt::Display for NodeRef { -> Result<(), std::fmt::Error> { match self { NodeRef::Simple(name) => name.val.fmt(f), - NodeRef::Sub(sub, name, _) => write!(f, - "{}.{}", - sub, - &name.val), + NodeRef::Sub(sub, op, name, _) => write!( + f, "{}{}{}", sub, + if *op == MemberOp::Arrow { "->" } else { "." }, + &name.val), + NodeRef::Index(inner) => write!(f, "{}[]", inner), } } } @@ -29,7 +43,8 @@ impl DMLNamed for NodeRef { fn name(&self) -> &DMLString { match self { Self::Simple(simple) => simple, - Self::Sub(_, sub, _) => sub, + Self::Sub(_, _, sub, _) => sub, + Self::Index(inner) => inner.name(), } } } @@ -37,7 +52,8 @@ impl DeclarationSpan for NodeRef { fn span(&self) -> &ZeroSpan { match self { Self::Simple(simple) => &simple.span, - Self::Sub(_, _, span) => span, + Self::Sub(_, _, _, span) => span, + Self::Index(inner) => inner.span(), } } } @@ -125,6 +141,8 @@ pub struct CodeReference { pub enum Reference { CodeReference(CodeReference), FileReference(DMLString), + // Member accessed on an expression that is not itself a reference + UntrackedMember(DMLString), } impl Reference { @@ -138,6 +156,7 @@ impl LocationSpan for Reference { match self { Reference::CodeReference(code_ref) => code_ref.loc_span(), Reference::FileReference(import) => &import.span, + Reference::UntrackedMember(member) => &member.span, } } } @@ -146,6 +165,7 @@ impl DeclarationSpan for Reference { match self { Reference::CodeReference(code_ref) => code_ref.span(), Reference::FileReference(import) => &import.span, + Reference::UntrackedMember(member) => &member.span, } } } diff --git a/src/analysis/structure/expressions.rs b/src/analysis/structure/expressions.rs index aa19b922..1167021f 100644 --- a/src/analysis/structure/expressions.rs +++ b/src/analysis/structure/expressions.rs @@ -543,6 +543,15 @@ pub enum IntegerLiteral { Unsigned(Value), } +impl IntegerLiteral { + pub fn value(&self) -> i128 { + match self { + IntegerLiteral::Signed(value) => value.val.into(), + IntegerLiteral::Unsigned(value) => value.val.into(), + } + } +} + impl DeclarationSpan for IntegerLiteral { fn span(&self) -> &ZeroSpan { match self { diff --git a/src/analysis/structure/objects.rs b/src/analysis/structure/objects.rs index 57f04156..529dae61 100644 --- a/src/analysis/structure/objects.rs +++ b/src/analysis/structure/objects.rs @@ -950,7 +950,9 @@ impl Scope for CompositeObject { self.statements.scopes() } fn defined_symbols(&self) -> Vec<&dyn StructureSymbol> { - let mut symbols = self.dims.to_symbols(); + let mut symbols: Vec<&dyn StructureSymbol> = self.dims.iter() + .filter(|dim|dim.indexvar.val != "_") + .map(|dim|dim as &dyn StructureSymbol).collect(); symbols.append(&mut self.statements.symbols()); symbols } @@ -1000,8 +1002,7 @@ pub struct Method { pub returns: Vec, pub modifier: MethodModifier, pub independent: bool, - // TODO: we could track startup here, but is there any - // reason to? + pub startup: bool, pub memoized: bool, pub default: bool, pub throws: bool, @@ -1090,6 +1091,7 @@ impl ToStructure for Method { let throws = content.throws.is_some(); let default = content.default.is_some(); let independent = content.independent.is_some(); + let startup = content.startup.is_some(); let memoized = content.memoized.is_some(); let modifier = if let Some(tok) = &content.modifier { // This token shouldnt actually be able to be missing @@ -1108,7 +1110,7 @@ impl ToStructure for Method { content.statements.collect_references(&mut references, file); Some(Method { object, arguments, returns, - independent, memoized, + independent, startup, memoized, modifier, default, throws, body, references, }) @@ -1193,6 +1195,22 @@ impl DeclarationSpan for Initializer { } } +impl Initializer { + pub fn expression(&self) -> Option<&Expression> { + match &self.kind { + InitializerKind::One(expression) => Some(expression), + _ => None, + } + } + + pub fn list(&self) -> Option<&[Box]> { + match &self.kind { + InitializerKind::List(initializers) => Some(initializers), + _ => None, + } + } +} + impl From for Initializer { fn from(from: InitializerKind) -> Initializer { let span = match &from { @@ -1436,7 +1454,16 @@ pub fn to_variable_structure<'a>(decls: &structure::VarDecl, expr.as_ref(), ExpressionKind::FunctionCall(_)); } } - if !is_single_fun && vars.len() != values.len(){ + // Count from the AST, so that unconvertible items do not cascade + let declared = match decls { + structure::VarDecl::One(_) => 1, + structure::VarDecl::Many(_, decls, _) => decls.iter().count(), + }; + let initialized = init.with_content(|content|match content { + misc::InitializerContent::Single(_) => 1, + misc::InitializerContent::Tuple(_, list, _) => list.len(), + }, declared); + if !is_single_fun && declared != initialized { report.push(LocalDMLError { range: init.range(), description: "Wrong number of \ @@ -1539,6 +1566,12 @@ impl DMLNamed for Typedef { } } +impl StructureSymbol for Typedef { + fn kind(&self) -> DMLSymbolKind { + DMLSymbolKind::Typedef + } +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum DMLObject { Bitorder(Bitorder), diff --git a/src/analysis/structure/statements.rs b/src/analysis/structure/statements.rs index b63ce405..764baa8b 100644 --- a/src/analysis/structure/statements.rs +++ b/src/analysis/structure/statements.rs @@ -39,6 +39,28 @@ impl StructureSymbol for ForEachIdentifier { } } +// A name in 'after hook -> (a, b): ...', usable in the call arguments +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct MessageComponent(pub DMLString); + +impl DeclarationSpan for MessageComponent { + fn span(&self) -> &ZeroSpan { + self.0.span() + } +} + +impl DMLNamed for MessageComponent { + fn name(&self) -> &DMLString { + &self.0 + } +} + +impl StructureSymbol for MessageComponent { + fn kind(&self) -> DMLSymbolKind { + DMLSymbolKind::Local + } +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ForEach { pub identifier: ForEachIdentifier, @@ -463,7 +485,7 @@ pub enum AfterExpression { // Note: We allow None here in order to not double-report // cases where a non-identifier has been providied as a binding // (it would report both wrong number of bindings and a parser error) - Hook(Expression, Vec>), + Hook(Expression, Vec>), } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -492,13 +514,15 @@ impl After { AfterExpression::Hook( ExpressionKind::to_expression( hook, report, file)?, - vec![bind.read_leaf(file.file)]), + vec![DMLString::from_token(bind, file) + .map(MessageComponent)]), statement::AfterTiming::HookBindList(hook, _, _, binds, _) => AfterExpression::Hook( ExpressionKind::to_expression( hook, report, file)?, binds.iter() - .map(|(bind, _)|bind.read_leaf(file.file)) + .map(|(bind, _)|DMLString::from_token(bind, file) + .map(MessageComponent)) .collect()), })); let call = ExpressionKind::to_expression( @@ -516,6 +540,23 @@ impl DeclarationSpan for After { } } +impl SymbolContainer for After { + fn symbols(&self) -> Vec<&dyn StructureSymbol> { + self.message_components().map( + |component|component as &dyn StructureSymbol).collect() + } +} + +impl After { + pub fn message_components(&self) + -> impl Iterator { + match &self.after { + Some(AfterExpression::Hook(_, components)) => components.as_slice(), + _ => &[], + }.iter().flatten().filter(|component|component.0.val != "_") + } +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Return { pub ret: Option, @@ -1181,6 +1222,7 @@ impl SymbolContainer for StatementKind { StatementKind::TryCatch(stmnt) => stmnt.symbols(), StatementKind::VariableDecl(stmnt) => stmnt.symbols(), StatementKind::Compound(stmnt) => stmnt.symbols(), + StatementKind::After(stmnt) => stmnt.symbols(), _ => vec![], } } diff --git a/src/analysis/structure/toplevel.rs b/src/analysis/structure/toplevel.rs index 5e6887da..d3ce73e8 100644 --- a/src/analysis/structure/toplevel.rs +++ b/src/analysis/structure/toplevel.rs @@ -124,7 +124,7 @@ impl ExistCondition { // TODO/NOTE: For now this will only report for provably non-constant // expressions, which there are none. As evaluation improves we can // eventually flip this to report for non-provably constant expressions - if evaluated.constant == Some(false) { + if evaluated.constant() == Some(false) { // TODO: This can be improved to find the range of the nearest // sub-expression that is non-constant without blaming the whole expression // (probably as a related span) @@ -212,23 +212,50 @@ impl ExistCondition { } } +// The non-'#if' context that an '#if' block is nested in +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum HashIfParent { + TopLevel, + Object(CompObjectKind), + InEach, + Template, +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum StatementContext { - Object, - HashIfTrue, - HashIfElse, + Object(CompObjectKind), + HashIfTrue(HashIfParent), + HashIfElse(HashIfParent), InEach, Template, } +impl StatementContext { + pub fn hashif_parent(self) -> HashIfParent { + match self { + StatementContext::Object(kind) => HashIfParent::Object(kind), + StatementContext::HashIfTrue(parent) | + StatementContext::HashIfElse(parent) => parent, + StatementContext::InEach => HashIfParent::InEach, + StatementContext::Template => HashIfParent::Template, + } + } + + pub fn is_toplevel_hashif(self) -> bool { + matches!(self, + StatementContext::HashIfTrue(HashIfParent::TopLevel) | + StatementContext::HashIfElse(HashIfParent::TopLevel)) + } +} + impl Display for StatementContext { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "{}", match self { - StatementContext::HashIfTrue => "'#if'", - StatementContext::HashIfElse => "'#else'", - StatementContext::Object => "an object declaration", + StatementContext::HashIfTrue(_) => "'#if'", + StatementContext::HashIfElse(_) => "'#else'", + StatementContext::Object(_) => "an object declaration", StatementContext::InEach => "an 'in each' declaration", StatementContext::Template => "a 'template' definition", }) @@ -395,6 +422,8 @@ pub struct StatementSpec { pub errors: Vec>, pub constants: Vec>, pub externs: Vec, + // Only populated for top-level '#if' branches, moved into TopLevel + pub loggroups: Vec>, } impl SymbolContainer for StatementSpec { @@ -453,6 +482,7 @@ impl StatementSpec { exports: vec![], imports: vec![], templates: vec![], + loggroups: vec![], } } pub fn consume(mut self, @@ -468,7 +498,8 @@ impl StatementSpec { errors: &mut Vec>, templates: &mut Vec>, imports: &mut Vec>, - hooks: &mut Vec>) { + hooks: &mut Vec>, + loggroups: &mut Vec>) { objects.append(&mut self.objects); constants.append(&mut self.constants); sessions.append(&mut self.sessions); @@ -482,6 +513,7 @@ impl StatementSpec { imports.append(&mut self.imports); templates.append(&mut self.templates); hooks.append(&mut self.hooks); + loggroups.append(&mut self.loggroups); } } @@ -504,6 +536,7 @@ fn flatten_hashif_branch(context: StatementContext, let mut hooks = vec![]; let mut errors = vec![]; let mut templates = vec![]; + let mut loggroups = vec![]; for inst in &stmnts.instantiations { instantiations.push(ObjectDecl::with_condition( inst, &condition)); @@ -599,12 +632,17 @@ fn flatten_hashif_branch(context: StatementContext, imports.push(ObjectDecl::with_condition( import, &condition)), DMLObject::Loggroup(loggroup) => - report.push(LocalDMLError { - range: loggroup.span.range, - description: format!( - "Loggroup declaration not allowed in {}", - context), - }), + if context.is_toplevel_hashif() { + loggroups.push(ObjectDecl::with_condition( + loggroup, &condition)); + } else { + report.push(LocalDMLError { + range: loggroup.span.range, + description: format!( + "Loggroup declaration not allowed in {}", + context), + }); + }, DMLObject::Typedef(typdef) => report.push(LocalDMLError { range: typdef.object.span.range, @@ -614,7 +652,7 @@ fn flatten_hashif_branch(context: StatementContext, }), DMLObject::CompositeObject(compobj) => { let subspec = flatten_hashif_branch( - StatementContext::Object, + StatementContext::Object(compobj.kind.kind), &compobj.statements, condition.clone(), report); objects.push( @@ -656,13 +694,13 @@ fn flatten_hashif_branch(context: StatementContext, sessions.push(ObjectDecl::with_condition( sess, &condition)), DMLObject::Parameter(param) => { - if matches!(context, StatementContext::HashIfElse | StatementContext::HashIfTrue) { + if matches!(context, StatementContext::HashIfElse(_) | StatementContext::HashIfTrue(_)) { report.push(LocalDMLError { range: *param.loc_range(), description: format!("Param declaration not allowed directly inside a {} block", match context { - StatementContext::HashIfElse => "#else", - StatementContext::HashIfTrue => "#if", + StatementContext::HashIfElse(_) => "#else", + StatementContext::HashIfTrue(_) => "#if", _ => unreachable!("Unexpected statement context in param declaration check"), }), }); @@ -680,12 +718,12 @@ fn flatten_hashif_branch(context: StatementContext, &ExistCondition::Conditional(Arc::new(vec![ (false, hi.condition.clone())]))); let truebranchspec = flatten_hashif_branch( - StatementContext::HashIfTrue, + StatementContext::HashIfTrue(context.hashif_parent()), &hi.truebranch, true_condition, report); let falsebranchspec = flatten_hashif_branch( - StatementContext::HashIfElse, + StatementContext::HashIfElse(context.hashif_parent()), &hi.falsebranch, false_condition, report); @@ -701,7 +739,8 @@ fn flatten_hashif_branch(context: StatementContext, &mut errors, &mut templates, &mut imports, - &mut hooks); + &mut hooks, + &mut loggroups); falsebranchspec.consume(&mut objects, &mut sessions, &mut saveds, @@ -714,7 +753,8 @@ fn flatten_hashif_branch(context: StatementContext, &mut errors, &mut templates, &mut imports, - &mut hooks); + &mut hooks, + &mut loggroups); }, } } @@ -733,6 +773,7 @@ fn flatten_hashif_branch(context: StatementContext, templates, imports, externs: vec![], + loggroups, } } @@ -746,7 +787,7 @@ pub struct TopLevel { pub device: Option, // These are all outside regular namespace, so makes sense to isolate them pub bitorder: Option, - pub loggroups: Vec, + pub loggroups: Vec>, pub cblocks: Vec, pub typedefs: Vec, pub references: Vec, @@ -786,7 +827,14 @@ impl Scope for TopLevel { self.spec.scopes() } fn defined_symbols(&self) -> Vec<&dyn StructureSymbol> { - self.spec.symbols() + let mut symbols = self.spec.symbols(); + symbols.extend(self.typedefs.to_symbols()); + symbols.extend(self.loggroups.to_symbols()); + symbols.extend(self.typedefs.iter().flat_map(|typedef| + typedef.typed.declaration_members().into_iter() + .filter(|member|member.name.is_some()) + .map(|member|member as &dyn StructureSymbol))); + symbols } } @@ -853,7 +901,7 @@ impl TopLevel { let mut ineachs = vec![]; for ineach in &statements.ineachs { - let spec = flatten_hashif_branch(StatementContext::Object, + let spec = flatten_hashif_branch(StatementContext::InEach, &ineach.statements, ExistCondition::Always, report); ineachs.push( @@ -899,7 +947,7 @@ impl TopLevel { description: "Bitorder declaration must follow a device declaration".to_string(), }), DMLObject::Loggroup(log) => - loggroups.push(log.clone()), + loggroups.push(ObjectDecl::always(log)), DMLObject::Constant(con) => constants.push(ObjectDecl::always(con)), DMLObject::Extern(ext) => @@ -926,7 +974,7 @@ impl TopLevel { typedefs.push(typedef.clone()), DMLObject::CompositeObject(compobj) => { let subspec = flatten_hashif_branch( - StatementContext::Object, + StatementContext::Object(compobj.kind.kind), &compobj.statements, ExistCondition::Always, report); objects.push( @@ -968,12 +1016,12 @@ impl TopLevel { let false_condition = ExistCondition::Conditional( Arc::new(vec![(false, hi.condition.clone())])); let truebranchspec = flatten_hashif_branch( - StatementContext::HashIfTrue, + StatementContext::HashIfTrue(HashIfParent::TopLevel), &hi.truebranch, true_condition, report); let falsebranchspec = flatten_hashif_branch( - StatementContext::HashIfElse, + StatementContext::HashIfElse(HashIfParent::TopLevel), &hi.falsebranch, false_condition, report); @@ -989,7 +1037,8 @@ impl TopLevel { &mut errors, &mut templates, &mut imports, - &mut hooks); + &mut hooks, + &mut loggroups); falsebranchspec.consume(&mut objects, &mut sessions, &mut saveds, @@ -1002,7 +1051,8 @@ impl TopLevel { &mut errors, &mut templates, &mut imports, - &mut hooks); + &mut hooks, + &mut loggroups); }, } } @@ -1010,7 +1060,7 @@ impl TopLevel { let spec = StatementSpec { objects, instantiations, ineachs, errors, imports, saveds, sessions, params, constants, methods, exports, hooks, - templates, externs, + templates, externs, loggroups: vec![], }; let mut references = vec![]; ast.references(&mut references, filespec); diff --git a/src/analysis/structure/types.rs b/src/analysis/structure/types.rs index b8e5e7a0..1a1e70b0 100644 --- a/src/analysis/structure/types.rs +++ b/src/analysis/structure/types.rs @@ -1,89 +1,408 @@ // © 2024 Intel Corporation // SPDX-License-Identifier: Apache-2.0 and MIT +use crate::analysis::parsing::lexer::TokenKind; +use crate::analysis::parsing::tree::{LeafToken, TreeElement, ZeroSpan}; use crate::analysis::parsing::{misc, types}; -use crate::analysis::parsing::tree::{ZeroSpan, TreeElement}; -use crate::analysis::LocalDMLError; -use crate::analysis::structure::expressions::DMLString; -use crate::analysis::FileSpec; - -// TODO/NOTE: Currently we use the range of the declaration in source -// as a placeholder for the actual type AST -pub type DMLType = ZeroSpan; - -pub fn deconstruct_typedecl<'a>( - content: &misc::TypeDeclContent, - _report: &mut Vec, - file: FileSpec<'a>) -> (Option, Option) { - // TODO: Correctly combine info from cdecl with this to construct actual - // type. Currently we just faux the type location as the entire typedecl +use crate::analysis::structure::expressions::{DMLString, Expression, ExpressionKind}; +use crate::analysis::symbols::{DMLSymbolKind, StructureSymbol}; +use crate::analysis::{FileSpec, LocalDMLError, LocationSpan, Named}; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DMLType { + pub kind: DMLTypeKind, + pub span: ZeroSpan, +} + +impl DMLType { + fn new(kind: DMLTypeKind, span: ZeroSpan) -> Self { + Self { kind, span } + } + + pub fn unknown(span: ZeroSpan) -> Self { + Self::new(DMLTypeKind::Unknown, span) + } + + pub fn span(&self) -> &ZeroSpan { + &self.span + } + + pub fn declaration_members(&self) -> Vec<&DMLTypeMember> { + let members = match &self.kind { + DMLTypeKind::Struct(members) | + DMLTypeKind::Layout { fields: members, .. } => members, + DMLTypeKind::Bitfields { fields, .. } => + return fields.iter().flat_map(|field| + std::iter::once(&field.member).chain( + field.member.typed.declaration_members())) + .collect(), + DMLTypeKind::Const(inner) | + DMLTypeKind::Pointer(inner) | + DMLTypeKind::Vector(inner) => + return inner.declaration_members(), + DMLTypeKind::Array { element, .. } => + return element.declaration_members(), + DMLTypeKind::Function { returns, .. } => + return returns.declaration_members(), + _ => return vec![], + }; + members.iter().flat_map(|member| + std::iter::once(member).chain( + member.typed.declaration_members())) + .collect() + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum DMLTypeKind { + Named(DMLString), + Struct(Vec), + Layout { + byte_order: Option, + fields: Vec, + }, + Bitfields { + width: Option, + fields: Vec, + }, + TypeOf(Expression), + Sequence(DMLString), + Hook(Vec), + Const(Box), + Pointer(Box), + Vector(Box), + Array { + element: Box, + size: Option, + }, + Function { + returns: Box, + parameters: Vec, + variadic: bool, + }, + Unknown, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DMLTypeMember { + pub name: Option, + pub typed: DMLType, + pub span: ZeroSpan, +} + +impl Named for DMLTypeMember { + fn get_name(&self) -> String { + self.name.as_ref().map(|name|name.val.clone()).unwrap_or_default() + } +} + +impl LocationSpan for DMLTypeMember { + fn loc_span(&self) -> &ZeroSpan { + self.name.as_ref().map(|name|&name.span).unwrap_or(&self.span) + } +} + +impl StructureSymbol for DMLTypeMember { + fn kind(&self) -> DMLSymbolKind { + DMLSymbolKind::Field + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DMLBitfieldMember { + pub member: DMLTypeMember, + pub range: DMLBitfieldRange, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum DMLBitfieldRange { + Single(Expression), + Range(Expression, Expression), + Unknown(ZeroSpan), +} + +fn span_of(element: &impl TreeElement, file: FileSpec<'_>) -> ZeroSpan { + ZeroSpan::from_range(element.range(), file.path) +} + +fn unknown_type(element: &impl TreeElement, file: FileSpec<'_>) -> DMLType { + DMLType::new(DMLTypeKind::Unknown, span_of(element, file)) +} + +fn wrap_type(kind: impl FnOnce(Box) -> DMLTypeKind, + typed: DMLType, + span: ZeroSpan) -> DMLType { + DMLType::new(kind(Box::new(typed)), span) +} + +fn apply_modifiers(modifiers: &[LeafToken], + mut typed: DMLType, + file: FileSpec<'_>) -> DMLType { + for modifier in modifiers { + let span = ZeroSpan::from_range( + crate::span::Range::combine(typed.span.range, modifier.range()), + file.path); + typed = match modifier.get_token().map(|token|token.kind) { + Some(TokenKind::Const) => + wrap_type(DMLTypeKind::Const, typed, span), + Some(TokenKind::Multiply) => + wrap_type(DMLTypeKind::Pointer, typed, span), + Some(TokenKind::Vect) => + wrap_type(DMLTypeKind::Vector, typed, span), + _ => DMLType::new(DMLTypeKind::Unknown, span), + }; + } + typed +} + +fn lower_member(decl: &misc::CDecl, + report: &mut Vec, + file: FileSpec<'_>) -> DMLTypeMember { + let span = span_of(decl, file); + let (name, typed) = decl.with_content( + |content|deconstruct_cdecl(content, report, file), + (None, None)); + DMLTypeMember { + name, + typed: typed.unwrap_or_else(||unknown_type(decl, file)), + span, + } +} + +fn lower_base_type(content: &types::BaseTypeContent, + report: &mut Vec, + file: FileSpec<'_>) -> DMLType { + let span = span_of(content, file); + let kind = match content { + types::BaseTypeContent::Ident(token) => + DMLString::from_token(token, file).map(DMLTypeKind::Named), + types::BaseTypeContent::StructType(struct_type) => Some( + DMLTypeKind::Struct(struct_type.members.iter() + .map(|(decl, _)|lower_member(decl, report, file)) + .collect())), + types::BaseTypeContent::Layout(layout) => Some(DMLTypeKind::Layout { + byte_order: layout.byteorder.read_leaf(file.file), + fields: layout.fields.iter() + .map(|(decl, _)|lower_member(decl, report, file)) + .collect(), + }), + types::BaseTypeContent::Bitfields(bitfields) => Some( + DMLTypeKind::Bitfields { + width: bitfields.iconst.read_leaf(file.file), + fields: bitfields.fields.iter().map(|field| { + let range = match &field.range { + types::BitfieldsRange::Expression(expression) => + ExpressionKind::to_expression( + expression, report, file) + .map(DMLBitfieldRange::Single), + types::BitfieldsRange::Range(first, _, last) => + ExpressionKind::to_expression(first, report, file) + .zip(ExpressionKind::to_expression( + last, report, file)) + .map(|(first, last)| + DMLBitfieldRange::Range(first, last)), + }.unwrap_or_else(||DMLBitfieldRange::Unknown( + span_of(&field.range, file))); + DMLBitfieldMember { + member: lower_member(&field.cdecl, report, file), + range, + } + }).collect(), + }), + types::BaseTypeContent::TypeOf(type_of) => + ExpressionKind::to_expression(&type_of.of, report, file) + .map(DMLTypeKind::TypeOf), + types::BaseTypeContent::Sequence(sequence) => + DMLString::from_token(&sequence.ident, file) + .map(DMLTypeKind::Sequence), + types::BaseTypeContent::HookType(hook) => Some(DMLTypeKind::Hook( + hook.args.iter().map(|(decl, _)| { + decl.with_content( + |content|deconstruct_cdecl(content, report, file).1, + None) + .unwrap_or_else(||unknown_type(decl, file)) + }).collect())), + }; + DMLType::new(kind.unwrap_or(DMLTypeKind::Unknown), span) +} + +fn lower_base(base: &types::BaseType, + report: &mut Vec, + file: FileSpec<'_>) -> DMLType { + base.with_content( + |content|lower_base_type(content, report, file), + unknown_type(base, file)) +} + +fn lower_type_decl(content: &misc::TypeDeclContent, + typed: DMLType, + report: &mut Vec, + file: FileSpec<'_>) -> (Option, DMLType) { match content { - misc::TypeDeclContent::Ident(tok) => - (DMLString::from_token(tok, file), - Some(ZeroSpan::from_range(content.range(), file.path))), - misc::TypeDeclContent::Array(inner_decl, _, _, _) => - match &inner_decl.content { - Some(inner_decl_obj) => { - // TODO: array the type - inner_decl_obj.with_content( - |con|deconstruct_typedecl(con, _report, file), - (None, None)) - }, - None => (None, Some(ZeroSpan::from_range(content.range(), - file.path))), - }, - misc::TypeDeclContent::Fun(inner_decl, _, _, _, _) => - match &inner_decl.content { - Some(inner_decl_obj) => { - // TODO: convert to actual function type - inner_decl_obj.with_content( - |con|deconstruct_typedecl(con, _report, file), - (None, None)) - }, - None => (None, Some( - ZeroSpan::from_range(content.range(), - file.path))), - }, - misc::TypeDeclContent::Parens(_, _, inner_decl, _) => - match &inner_decl.content { - Some(inner_decl_obj) => { - // TODO: modify with in-parens modifiers - inner_decl_obj.with_content( - |con|deconstruct_typedecl(con, _report, file), - (None, None)) - }, - None => (None, Some(ZeroSpan::from_range(content.range(), - file.path))), - }, + misc::TypeDeclContent::Ident(token) => + (DMLString::from_token(token, file), typed), + misc::TypeDeclContent::Array(inner, _, size, _) => { + let span = span_of(content, file); + let size = ExpressionKind::to_expression(size, report, file); + let array = DMLType::new(DMLTypeKind::Array { + element: Box::new(typed), + size, + }, span); + lower_optional_type_decl(inner, array, report, file) + }, + misc::TypeDeclContent::Fun(inner, _, parameters, ellipsis, _) => { + let span = span_of(content, file); + let parameters = parameters.iter().map(|(decl, _)| { + decl.with_content( + |content|deconstruct_cdecl(content, report, file).1, + None) + .unwrap_or_else(||unknown_type(decl, file)) + }).collect(); + let function = DMLType::new(DMLTypeKind::Function { + returns: Box::new(typed), + parameters, + variadic: ellipsis.is_some(), + }, span); + lower_optional_type_decl(inner, function, report, file) + }, + misc::TypeDeclContent::Parens(_, modifiers, inner, _) => { + let modified = apply_modifiers(modifiers, typed, file); + lower_optional_type_decl(inner, modified, report, file) + }, + } +} + +fn lower_optional_type_decl(decl: &misc::TypeDecl, + typed: DMLType, + report: &mut Vec, + file: FileSpec<'_>) -> (Option, DMLType) { + match &decl.content { + Some(content) => content.with_content( + |content|lower_type_decl(content, typed.clone(), report, file), + (None, typed.clone())), + None => (None, typed), } } -// TODO: For now this does not need to obtain the actual type, only the name -// of the declaration pub fn deconstruct_cdecl<'a>(content: &misc::CDeclContent, report: &mut Vec, - file: FileSpec<'a>) -> - (Option, Option) { - // let is_const = content.consttok.is_some(); - // let base = TODO; - // let modififiers = TODO; - match &content.decl.content { - Some(typedeclobj) => { - // I _think_ a typedecl can never be an empty object, (since an - // empty typedecl will just become None) - typedeclobj.with_content( - |con|deconstruct_typedecl(con, report, file), - (None, None)) - }, - None => (None, Some(ZeroSpan::from_range(content.base.range(), - file.path))), + file: FileSpec<'a>) + -> (Option, Option) { + let mut typed = lower_base(&content.base, report, file); + if content.consttok.is_some() { + let span = span_of(content, file); + typed = wrap_type(DMLTypeKind::Const, typed, span); } + typed = apply_modifiers(&content.modifiers, typed, file); + let (name, mut typed) = lower_optional_type_decl( + &content.decl, typed, report, file); + typed.span = span_of(content, file); + (name, Some(typed)) +} + +fn lower_simple_ctype(simple: &types::CTypeDeclSimple, + typed: DMLType, + file: FileSpec<'_>) -> DMLType { + simple.with_content(|content| { + let modified = apply_modifiers(&content.modifiers, typed.clone(), file); + content.inner.as_ref().map_or(modified.clone(), |(_, inner, _)| + lower_simple_ctype(inner, modified, file)) + }, typed.clone()) } #[allow(clippy::ptr_arg)] pub fn to_type<'a>(content: &types::CTypeDecl, - _report: &mut Vec, + report: &mut Vec, file: FileSpec<'a>) -> Option { - content.with_content(|con|Some(ZeroSpan::from_range(con.range(), - file.path)), - None) + content.with_content(|content| { + let mut typed = lower_base(&content.base, report, file); + if content.consttok.is_some() { + typed = wrap_type(DMLTypeKind::Const, typed, span_of(content, file)); + } + let mut typed = lower_simple_ctype(&content.simple, typed, file); + typed.span = span_of(content, file); + Some(typed) + }, None) +} + +#[cfg(test)] +mod tests { + use std::path::Path; + use std::str::FromStr; + + use logos::Logos; + + use super::*; + use crate::analysis::parsing::lexer::TokenKind; + use crate::analysis::parsing::parser::{ + doesnt_understand_tokens, FileInfo, FileParser, Parse, ParseContext, + }; + use crate::vfs::TextFile; + + fn lower_cdecl(source: &str) -> (Option, DMLType) { + let lexer = TokenKind::lexer(source); + let mut parser = FileParser::new(lexer); + let context = ParseContext::new_context(doesnt_understand_tokens); + let ast = misc::CDecl::parse(&context, &mut parser, &FileInfo::default()); + assert!(parser.skipped_tokens.is_empty(), + "unexpected skipped tokens: {:?}", parser.skipped_tokens); + + let text = TextFile::from_str(source).unwrap(); + let path = Path::new("type-test.dml"); + let file = FileSpec { path, file: &text }; + let mut errors = vec![]; + let lowered = ast.with_content( + |content|deconstruct_cdecl(content, &mut errors, file), + (None, None)); + assert!(errors.is_empty(), "unexpected lowering errors: {:?}", errors); + (lowered.0, lowered.1.expect("declaration should have a type")) + } + + fn assert_named(typed: &DMLType, expected: &str) { + match &typed.kind { + DMLTypeKind::Named(name) => assert_eq!(name.val, expected), + other => panic!("expected named type, got {:?}", other), + } + } + + #[test] + fn lowers_array_of_pointers_with_correct_precedence() { + let (name, typed) = lower_cdecl("uint32 *items[4]"); + assert_eq!(name.unwrap().val, "items"); + match typed.kind { + DMLTypeKind::Array { element, size: Some(_) } => match element.kind { + DMLTypeKind::Pointer(inner) => assert_named(&inner, "uint32"), + other => panic!("expected pointer element, got {:?}", other), + }, + other => panic!("expected array type, got {:?}", other), + } + } + + #[test] + fn lowers_pointer_to_array_with_correct_precedence() { + let (name, typed) = lower_cdecl("uint32 (*items)[4]"); + assert_eq!(name.unwrap().val, "items"); + match typed.kind { + DMLTypeKind::Pointer(inner) => match inner.kind { + DMLTypeKind::Array { element, size: Some(_) } => + assert_named(&element, "uint32"), + other => panic!("expected array target, got {:?}", other), + }, + other => panic!("expected pointer type, got {:?}", other), + } + } + + #[test] + fn retains_anonymous_struct_fields() { + let (name, typed) = lower_cdecl( + "struct { uint32 addr; uint8 data[16]; } packet"); + assert_eq!(name.unwrap().val, "packet"); + let DMLTypeKind::Struct(fields) = typed.kind else { + panic!("expected struct type, got {:?}", typed.kind); + }; + assert_eq!(fields.len(), 2); + assert_eq!(fields[0].name.as_ref().unwrap().val, "addr"); + assert_named(&fields[0].typed, "uint32"); + assert_eq!(fields[1].name.as_ref().unwrap().val, "data"); + assert!(matches!(fields[1].typed.kind, DMLTypeKind::Array { .. })); + } } diff --git a/src/analysis/symbols.rs b/src/analysis/symbols.rs index 3ca08a46..e075e413 100644 --- a/src/analysis/symbols.rs +++ b/src/analysis/symbols.rs @@ -13,7 +13,7 @@ use crate::analysis::structure::objects::{CompObjectKind}; use crate::analysis::structure::expressions::DMLString; use crate::analysis::templating::objects::{DMLObject, DMLNamedMember, StructureKey}; use crate::analysis::templating::methods::{DMLMethodRef}; -use crate::analysis::templating::types::DMLResolvedType; +use crate::analysis::templating::types::{TypeId, TypeUse}; use crate::analysis::templating::traits::DMLTemplate; #[derive(PartialEq, Eq, Debug, Clone, Copy, Hash, PartialOrd, Ord)] @@ -22,6 +22,7 @@ pub enum DMLSymbolKind { Parameter, Constant, Extern, + Field, Hook, Local, Loggroup, @@ -119,8 +120,11 @@ pub enum SymbolSource { MethodArg(Arc, DMLString), MethodLocal(Arc, DMLString), // TODO: RC this if it's expensive - Type(DMLResolvedType), + Type(TypeId), + // (type containing the member, member name location) + TypeMember(TypeUse, ZeroSpan), Template(Arc), + Loggroup(DMLString), } impl std::fmt::Debug for SymbolSource { @@ -146,10 +150,18 @@ impl std::fmt::Debug for SymbolSource { SymbolSource::Type(_) => f.debug_struct("SymbolSource::Type") .finish_non_exhaustive(), + SymbolSource::TypeMember(_, location) => + f.debug_struct("SymbolSource::TypeMember") + .field("location", location) + .finish(), SymbolSource::Template(templ) => f.debug_struct("SymbolSource::Template") .field("name", &templ.name) .finish_non_exhaustive(), + SymbolSource::Loggroup(name) => + f.debug_struct("SymbolSource::Loggroup") + .field("name", &name.val) + .finish(), } } } @@ -174,9 +186,10 @@ impl SymbolSource { _ => None } } - pub fn as_type(&self) -> Option<&DMLResolvedType> { + pub fn as_type(&self) -> Option { match self { - Self::Type(typ) => Some(typ), + Self::Type(typ) => Some(*typ), + Self::TypeMember(TypeUse::Declared(typ), _) => Some(*typ), _ => None } } @@ -203,7 +216,9 @@ impl SymbolSource { SymbolSource::MethodArg(_, name) => Some(&name.val), SymbolSource::MethodLocal(_, name) => Some(&name.val), SymbolSource::Type(_) => None, + SymbolSource::TypeMember(_, _) => None, SymbolSource::Template(templ) => Some(&templ.name), + SymbolSource::Loggroup(name) => Some(&name.val), } } } @@ -233,8 +248,15 @@ pub struct Symbol { // some param or method set in them pub implementations: HashSet, pub source: SymbolSource, + // Explicit type declarations denoted by this syntax without implying + // that the symbol itself has a value type. + pub type_definitions: HashSet, // TODO: RC or box this if it's expensive - pub typed: Option, + pub typed: Option, + // Pointer and array layers wrapping 'typed' in the declared type + pub indirections: u32, + // Declared with a 'typeof' type, which leaves 'typed' unresolved + pub typeof_declared: bool, } // We do this often enough to warrant a reusable pattern, @@ -355,7 +377,10 @@ impl Symbol { references: HashSet::default(), implementations: HashSet::default(), bases: Vec::default(), + type_definitions: HashSet::default(), typed: None, + indirections: 0, + typeof_declared: false, } } diff --git a/src/analysis/templating/evaluation.rs b/src/analysis/templating/evaluation.rs index 051bb568..4ea3e51b 100644 --- a/src/analysis/templating/evaluation.rs +++ b/src/analysis/templating/evaluation.rs @@ -6,7 +6,14 @@ use lsp_types::DiagnosticSeverity; use crate::analysis::{DMLError, DeclarationSpan}; -use crate::analysis::templating::types::DMLResolvedType; +use crate::analysis::structure::expressions::{ExpressionKind, FunctionCall, + IndexExpression, MemberLiteral, + MemberOp}; +use crate::analysis::structure::types::DMLType; +use crate::analysis::templating::types::{DMLConcreteType, + DMLConcreteTypeKind, + DMLResolvedType}; +use crate::analysis::parsing::tree::ZeroSpan; use std::collections::HashSet; @@ -37,14 +44,14 @@ pub enum EvaluatedValue { // We'll use this for storing both signed, unsigned, and endian values // the disambiguation between them is done by the type in the // EvaluationResult struct - Int(i64), + Int(i128), Bool(bool), DMLObject(()), // TODO: How to specify this? Path? Arc? // TODO: Other, more obtuse, types (floats, pointers, strings?) } impl EvaluatedValue { - pub fn as_int(&self) -> Option { + pub fn as_int(&self) -> Option { match self { EvaluatedValue::Int(i) => Some(*i), _ => None, @@ -59,33 +66,143 @@ impl EvaluatedValue { } } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct EvaluationOrigin { + pub identity: String, + pub location: ZeroSpan, +} + #[derive(Debug, Clone)] -pub struct EvaluationResult { +pub struct EvaluationSlot { pub value: Option, - // Whether this value is 'true constant' and can be used to resolve #if's at compile time pub constant: Option, pub typed: Option, } -impl EvaluationResult { - pub fn empty() -> Self { - EvaluationResult { +impl EvaluationSlot { + pub fn unknown() -> Self { + Self { value: None, constant: None, typed: None, } } - pub fn as_int(&self) -> Option { - self.value.as_ref().and_then(EvaluatedValue::as_int) + + pub fn from_type(typed: DMLResolvedType) -> Self { + Self { + value: None, + constant: None, + typed: Some(typed), + } + } +} + +#[derive(Debug, Clone)] +pub enum EvaluationResult { + Known { + slots: Vec, + origin: Option, + }, + UnknownArity, +} + +impl EvaluationResult { + pub fn empty() -> Self { + Self::Known { + slots: vec![EvaluationSlot::unknown()], + origin: None, + } + } + + pub fn single(slot: EvaluationSlot) -> Self { + Self::Known { slots: vec![slot], origin: None } + } + + pub fn slot(&self) -> Option<&EvaluationSlot> { + match self { + Self::Known { slots, .. } if slots.len() == 1 => slots.first(), + _ => None, + } + } + + pub fn origin(&self) -> Option<&EvaluationOrigin> { + match self { + Self::Known { origin, .. } => origin.as_ref(), + Self::UnknownArity => None, + } + } + + pub fn as_int(&self) -> Option { + self.slot()?.value.as_ref().and_then(EvaluatedValue::as_int) } pub fn as_bool(&self) -> Option { - self.value.as_ref().and_then(EvaluatedValue::as_bool) + self.slot()?.value.as_ref().and_then(EvaluatedValue::as_bool) + } + + pub fn constant(&self) -> Option { + self.slot()?.constant + } + + pub fn with_constant(mut self, constant: Option) -> Self { + if let Self::Known { slots, .. } = &mut self { + if let [slot] = slots.as_mut_slice() { + slot.constant = constant; + } + } + self + } + + pub fn with_type(mut self, typed: DMLResolvedType) -> Self { + if let Self::Known { slots, .. } = &mut self { + if let [slot] = slots.as_mut_slice() { + slot.typed = Some(typed); + } + } + self + } +} + +pub trait ExpressionResolver { + fn identifier_type(&mut self, _name: &str) -> Option { + None + } + + fn resolve_type( + &mut self, + _typed: &DMLType, + _context: &mut EvaluationContext, + _report: &mut Vec, + ) -> Option { + None + } + + fn function_call( + &mut self, + _call: &FunctionCall, + _context: &mut EvaluationContext, + _report: &mut Vec, + ) -> EvaluationResult { + EvaluationResult::UnknownArity } } +struct NoExpressionResolver; + +impl ExpressionResolver for NoExpressionResolver {} + pub trait Evaluatable { - fn evaluate(&self, context: &mut EvaluationContext, report: &mut Vec) -> EvaluationResult; + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult; + + fn evaluate(&self, context: &mut EvaluationContext, + report: &mut Vec) -> EvaluationResult { + self.evaluate_with(context, &mut NoExpressionResolver, report) + } } // TODO: As we make improvements, we can add Evaluatable trait impls @@ -93,51 +210,199 @@ pub trait Evaluatable { // Note: we fully qualify the types here, as we may want to implement this trait for similarly-named // types from different stages in the analysis -impl Evaluatable for crate::analysis::structure::expressions::Expression { - fn evaluate(&self, context: &mut EvaluationContext, report: &mut Vec) -> EvaluationResult { - match self.as_ref() { - crate::analysis::structure::expressions::ExpressionKind::TertiaryExpression(texpr) => texpr.evaluate(context, report), - crate::analysis::structure::expressions::ExpressionKind::BinaryExpression(binexpr) => binexpr.evaluate(context, report), - crate::analysis::structure::expressions::ExpressionKind::UnaryExpression(uexpr) => uexpr.evaluate(context, report), - crate::analysis::structure::expressions::ExpressionKind::IndexExpression(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::Slice(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::Identifier(ident) => ident.evaluate(context, report), - crate::analysis::structure::expressions::ExpressionKind::IntegerLiteral(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::StringLiteral(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::CharLiteral(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::MemberLiteral(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::FloatLiteral(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::FunctionCall(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::CastExpression(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::NewExpression(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::SizeOf(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::SizeOfType(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::ConstantList(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::EachIn(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::AutoObjectRef(_, _) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::Undefined(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::ExpressionKind::Unknown(_) => EvaluationResult::empty(), +impl Evaluatable for ExpressionKind { + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let span = *self.span(); + let named = |name: &str|DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Named(name.to_string()), + span, + }); + match self { + ExpressionKind::TertiaryExpression(expression) => + expression.evaluate_with(context, resolver, report), + ExpressionKind::BinaryExpression(expression) => + expression.evaluate_with(context, resolver, report), + ExpressionKind::UnaryExpression(expression) => + expression.evaluate_with(context, resolver, report), + ExpressionKind::Identifier(identifier) => + identifier.evaluate_with(context, resolver, report), + ExpressionKind::IntegerLiteral(integer) => + EvaluationResult::single(EvaluationSlot { + value: Some(EvaluatedValue::Int(integer.value())), + constant: Some(true), + typed: Some(named(match integer { + crate::analysis::structure::expressions::IntegerLiteral::Signed(_) => + "int64", + crate::analysis::structure::expressions::IntegerLiteral::Unsigned(_) => + "uint64", + })), + }), + ExpressionKind::FloatLiteral(_) => + typed_slot(named("double")), + ExpressionKind::CharLiteral(_) => + typed_slot(named("char")), + ExpressionKind::StringLiteral(_) => EvaluationResult::single( + EvaluationSlot { + value: None, + constant: Some(true), + typed: Some(DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Pointer(Box::new( + DMLConcreteType { + kind: DMLConcreteTypeKind::Const(Box::new( + DMLConcreteType { + kind: DMLConcreteTypeKind::Named( + "char".to_string()), + span, + })), + span, + })), + span, + })), + }), + ExpressionKind::FunctionCall(call) => + resolver.function_call(call, context, report), + ExpressionKind::MemberLiteral(member) => + member.evaluate_with(context, resolver, report), + ExpressionKind::IndexExpression(index) => + index.evaluate_with(context, resolver, report), + ExpressionKind::CastExpression(cast) => { + let _ = cast.from.evaluate_with(context, resolver, report); + resolver.resolve_type(&cast.to, context, report) + .map(typed_slot) + .unwrap_or_else(EvaluationResult::empty) + }, + _ => EvaluationResult::empty(), } } } +fn unqualified_type(typed: &DMLConcreteType) -> &DMLConcreteType { + match &typed.kind { + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } | DMLConcreteTypeKind::Const(underlying) => + unqualified_type(underlying), + _ => typed, + } +} + +impl Evaluatable for MemberLiteral { + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let evaluated = self.left.evaluate_with(context, resolver, report); + let Some(DMLResolvedType::Actual(left)) = evaluated.slot() + .and_then(|slot|slot.typed.as_ref()) + else { + return EvaluationResult::empty(); + }; + let aggregate = match self.operator { + MemberOp::Dot => unqualified_type(left), + MemberOp::Arrow => { + let DMLConcreteTypeKind::Pointer(inner) = + &unqualified_type(left).kind + else { + return EvaluationResult::empty(); + }; + unqualified_type(inner) + }, + }; + let (fields, aggregate_name) = match &aggregate.kind { + DMLConcreteTypeKind::Struct { fields, .. } => (fields, "Struct"), + DMLConcreteTypeKind::Layout { fields, .. } => (fields, "Layout"), + DMLConcreteTypeKind::Bitfields { fields, .. } => + (fields, "Bitfields"), + _ => return EvaluationResult::empty(), + }; + let Some(field) = fields.iter().find(|field| + field.name.as_deref() == Some(self.right.val.as_str())) + else { + context.report_if_new(DMLError { + span: self.right.span, + description: format!( + "{} type has no field named '{}'", + aggregate_name, self.right.val), + related: vec![], + severity: Some(DiagnosticSeverity::ERROR), + }, report); + return EvaluationResult::empty(); + }; + EvaluationResult::single(EvaluationSlot::from_type( + DMLResolvedType::Actual(field.typed.clone()))) + } +} + +impl Evaluatable for IndexExpression { + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let evaluated = self.array.evaluate_with(context, resolver, report); + let _ = self.index.evaluate_with(context, resolver, report); + let Some(DMLResolvedType::Actual(array)) = evaluated.slot() + .and_then(|slot|slot.typed.as_ref()) + else { + return EvaluationResult::empty(); + }; + let element = match &unqualified_type(array).kind { + DMLConcreteTypeKind::Array { element, .. } | + DMLConcreteTypeKind::Pointer(element) | + DMLConcreteTypeKind::Vector(element) => element, + _ => return EvaluationResult::empty(), + }; + EvaluationResult::single(EvaluationSlot::from_type( + DMLResolvedType::Actual((**element).clone()))) + } +} + +impl Evaluatable for crate::analysis::structure::expressions::Expression { + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + self.as_ref().evaluate_with(context, resolver, report) + } +} + +fn typed_slot(typed: DMLResolvedType) -> EvaluationResult { + EvaluationResult::single(EvaluationSlot::from_type(typed)) +} + fn not_operation(inner: EvaluationResult) -> EvaluationResult { // For now, we only support the not operator for boolean values, but ! is applicable to // other types as well // TODO: We don't have a type system yet, but when we do that should guide the semantics of this - match inner.value { - Some(EvaluatedValue::Bool(b)) => EvaluationResult { - value: Some(EvaluatedValue::Bool(!b)), - constant: inner.constant, - typed: inner.typed, - }, + match inner.as_bool() { + Some(value) => EvaluationResult::single(EvaluationSlot { + value: Some(EvaluatedValue::Bool(!value)), + constant: inner.constant(), + typed: inner.slot().and_then(|slot|slot.typed.clone()), + }), _ => EvaluationResult::empty(), } } impl Evaluatable for crate::analysis::structure::expressions::UnaryExpression { - fn evaluate(&self, context: &mut EvaluationContext, report: &mut Vec) -> EvaluationResult { - let inner_evaluated = self.operand.evaluate(context, report); + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let inner_evaluated = self.operand.evaluate_with( + context, resolver, report); // Break out operations to subfunctions so as to not clutter this function too much match self.operator { crate::analysis::structure::expressions::UnaryOp::Not => not_operation(inner_evaluated), @@ -153,45 +418,68 @@ impl Evaluatable for crate::analysis::structure::expressions::UnaryExpression { } impl Evaluatable for crate::analysis::structure::expressions::Identifier { - fn evaluate(&self, _context: &mut EvaluationContext, _report: &mut Vec) -> EvaluationResult { + fn evaluate_with( + &self, + _context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + _report: &mut Vec, + ) -> EvaluationResult { // TODO: eventually we will do symbol lookups based on the evaluation context, // for now we hard-code some constants which are treated especially by DMLC + let boolean_type = ||DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Named("bool".to_string()), + span: self.name.span, + }); match self.name.val.as_str() { - "true" => EvaluationResult { + "true" => EvaluationResult::single(EvaluationSlot { value: Some(EvaluatedValue::Bool(true)), constant: Some(true), - typed: None, - }, - "false" => EvaluationResult { + typed: Some(boolean_type()), + }), + "false" => EvaluationResult::single(EvaluationSlot { value: Some(EvaluatedValue::Bool(false)), constant: Some(true), - typed: None, - }, - "dml_1_2" => EvaluationResult { + typed: Some(boolean_type()), + }), + "dml_1_2" => EvaluationResult::single(EvaluationSlot { value: Some(EvaluatedValue::Bool(false)), constant: Some(true), - typed: None, - }, - "dml_1_4" => EvaluationResult { + typed: Some(boolean_type()), + }), + "dml_1_4" => EvaluationResult::single(EvaluationSlot { value: Some(EvaluatedValue::Bool(true)), constant: Some(true), - typed: None, - }, - _ => EvaluationResult::empty(), + typed: Some(boolean_type()), + }), + name => resolver.identifier_type(name) + .map(typed_slot) + .unwrap_or_else(EvaluationResult::empty), } } } impl Evaluatable for crate::analysis::structure::expressions::BinaryExpression { - fn evaluate(&self, context: &mut EvaluationContext, report: &mut Vec) -> EvaluationResult { - let left_evaluated = self.left.evaluate(context, report); - let right_evaluated = self.right.evaluate(context, report); + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let left_evaluated = self.left.evaluate_with( + context, resolver, report); + let right_evaluated = self.right.evaluate_with( + context, resolver, report); + let boolean_type = DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Named("bool".to_string()), + span: self.span, + }); match &self.operator { crate::analysis::structure::expressions::BinOp::Math(_) => EvaluationResult::empty(), - crate::analysis::structure::expressions::BinOp::Comp(_) => EvaluationResult::empty(), + crate::analysis::structure::expressions::BinOp::Comp(_) => + typed_slot(boolean_type), crate::analysis::structure::expressions::BinOp::Logic(logic_op) => logic_expression( left_evaluated, right_evaluated, logic_op, report, - ), + ).with_type(boolean_type), } } } @@ -222,51 +510,59 @@ fn logic_expression(left: EvaluationResult, crate::analysis::structure::expressions::LogicOp::And => { match (left.as_bool(), right.as_bool()) { // TODO: Verify that DMLC short-circuiting actually short-circuits the constant property like this - (Some(false), _) => (left.constant, Some(false)), - (_, Some(false)) => (right.constant, Some(false)), - (Some(b1), Some(b2)) => (combine_constants(&[left.constant, right.constant]), Some(b1 && b2)), + (Some(false), _) => (left.constant(), Some(false)), + (_, Some(false)) => (right.constant(), Some(false)), + (Some(b1), Some(b2)) => (combine_constants( + &[left.constant(), right.constant()]), Some(b1 && b2)), _ => (None, None), } }, crate::analysis::structure::expressions::LogicOp::Or => { match (left.as_bool(), right.as_bool()) { - (Some(false), Some(false)) => (combine_constants(&[left.constant, right.constant]), Some(false)), - (Some(true), _) => (left.constant, Some(true)), - (_, Some(true)) => (right.constant, Some(true)), + (Some(false), Some(false)) => (combine_constants( + &[left.constant(), right.constant()]), Some(false)), + (Some(true), _) => (left.constant(), Some(true)), + (_, Some(true)) => (right.constant(), Some(true)), _ => (None, None), } }, }; - EvaluationResult { + EvaluationResult::single(EvaluationSlot { value: logic_result.map(EvaluatedValue::Bool), constant, typed: None, - } + }) } impl Evaluatable for crate::analysis::structure::expressions::TertiaryExpression { - fn evaluate(&self, context: &mut EvaluationContext, report: &mut Vec) -> EvaluationResult { - let left_evaluated = self.left.evaluate(context, report); + fn evaluate_with( + &self, + context: &mut EvaluationContext, + resolver: &mut dyn ExpressionResolver, + report: &mut Vec, + ) -> EvaluationResult { + let left_evaluated = self.left.evaluate_with( + context, resolver, report); // Patch in the constant-ness of the condition, as that will modify the constant-ness of the result - let mut middle_evaluated = self.middle.evaluate(context, report); - let mut right_evaluated = self.right.evaluate(context, report); - middle_evaluated.constant = combine_constants(&[left_evaluated.constant, middle_evaluated.constant]); - right_evaluated.constant = combine_constants(&[left_evaluated.constant, right_evaluated.constant]); + let middle_evaluated = self.middle.evaluate_with( + context, resolver, report); + let right_evaluated = self.right.evaluate_with( + context, resolver, report); + let middle_constant = combine_constants( + &[left_evaluated.constant(), middle_evaluated.constant()]); + let right_constant = combine_constants( + &[left_evaluated.constant(), right_evaluated.constant()]); match &self.operator { crate::analysis::structure::expressions::TertiaryOp::Cond => if left_evaluated.as_bool() == Some(true) { - middle_evaluated + middle_evaluated.with_constant(middle_constant) } else if left_evaluated.as_bool() == Some(false) { - right_evaluated + right_evaluated.with_constant(right_constant) } else { - EvaluationResult { - value: None, - constant: None, - typed: None, - } + EvaluationResult::empty() }, crate::analysis::structure::expressions::TertiaryOp::HashCond => { - if left_evaluated.constant == Some(false) { + if left_evaluated.constant() == Some(false) { context.report_if_new( DMLError { span: *self.left.span(), @@ -276,15 +572,11 @@ impl Evaluatable for crate::analysis::structure::expressions::TertiaryExpression }, report); } if left_evaluated.as_bool() == Some(true) { - middle_evaluated + middle_evaluated.with_constant(middle_constant) } else if left_evaluated.as_bool() == Some(false) { - right_evaluated + right_evaluated.with_constant(right_constant) } else { - EvaluationResult { - value: None, - constant: None, - typed: None, - } + EvaluationResult::empty() } }, } diff --git a/src/analysis/templating/methods.rs b/src/analysis/templating/methods.rs index bf57a198..2097a36c 100644 --- a/src/analysis/templating/methods.rs +++ b/src/analysis/templating/methods.rs @@ -2,20 +2,39 @@ // SPDX-License-Identifier: Apache-2.0 and MIT use std::sync::Arc; use lsp_types::DiagnosticSeverity; +use std::collections::HashMap; use crate::analysis::parsing::tree::ZeroSpan; use crate::analysis::symbols::{DMLSymbolKind, MakeSymbolContainer, StructureSymbol, SymbolContainer}; -use crate::analysis::structure::expressions::DMLString; +use crate::analysis::structure::expressions::{DMLString, ExpressionKind, + FunctionCall, Initializer, + SingleInitializer}; use crate::analysis::structure::types::DMLType; use crate::analysis::structure::objects::{MaybeAbstract, MethodArgument, - MethodModifier, Method}; -use crate::analysis::structure::statements::{Statement, StatementKind}; + MethodModifier, Method, + VariableDeclKind}; +use crate::analysis::structure::statements::{Assigner, ForPre, Statement, + StatementKind, SwitchCase}; use crate::analysis::{DeclarationSpan, DMLNamed, DMLError}; +use crate::analysis::reference::{NodeRef, Reference}; use crate::analysis::templating::Declaration; -use crate::analysis::templating::objects::DMLNamedMember; -use crate::analysis::templating::types::{eval_type_simple, DMLResolvedType}; -use crate::analysis::templating::traits::{DMLTrait}; +use crate::analysis::templating::evaluation::{Evaluatable, EvaluatedValue, + EvaluationContext, + EvaluationOrigin, + EvaluationResult, + EvaluationSlot, + ExpressionResolver}; +use crate::analysis::templating::objects::{DMLCompositeObject, DMLNamedMember}; +use crate::analysis::templating::types::{check_storage_compatibility, + eval_type_simple, + eval_type_with_expression, + DMLConcreteType, + DMLStructType, + DMLResolvedType, + StorageCompatibility, StorageValue, + TypeDatabase}; +use crate::analysis::templating::traits::{lookup_impl, DMLTrait}; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum DMLMethodArg { @@ -47,7 +66,7 @@ impl DMLMethodArg { (DMLMethodArg::Inline(_), DMLMethodArg::Inline(_)) => true, (DMLMethodArg::Typed(decl1), DMLMethodArg::Typed(decl2)) => - decl1.type_ref.equivalent(&decl2.type_ref), + decl1.type_ref.equivalent_unqualified(&decl2.type_ref), _ => false } } @@ -59,13 +78,15 @@ impl DMLMethodArg { } } -pub fn eval_method_args(args: &[MethodArgument], report: &mut Vec) +pub fn eval_method_args(args: &[MethodArgument], types: &TypeDatabase, + report: &mut Vec) -> Vec { args.iter().map(|arg| match arg { MethodArgument::Typed(name, typed) => { + types.report_unknown_types(typed, report); let (structs, type_ref) = eval_type_simple( - typed, (), ()); + typed, types); for s in &structs { report.push( DMLError { @@ -84,6 +105,7 @@ pub fn eval_method_args(args: &[MethodArgument], report: &mut Vec) DMLResolvedType::Dummy( *type_ref.span()) }, + type_syntax: typed.clone(), name: name.clone() }) }, @@ -92,10 +114,12 @@ pub fn eval_method_args(args: &[MethodArgument], report: &mut Vec) }).collect() } -pub fn eval_method_returns(returns: &[DMLType], report: &mut Vec) +pub fn eval_method_returns(returns: &[DMLType], types: &TypeDatabase, + report: &mut Vec) -> Vec { returns.iter().map(|ret| { - let (structs, type_ref) = eval_type_simple(ret, (), ()); + types.report_unknown_types(ret, report); + let (structs, type_ref) = eval_type_simple(ret, types); for s in &structs { report.push( DMLError { @@ -114,16 +138,1153 @@ pub fn eval_method_returns(returns: &[DMLType], report: &mut Vec) }).collect() } +#[derive(Clone)] +struct MethodBinding { + typed: Option, + declaration_span: ZeroSpan, + writable: bool, +} + +type MethodEnvironment = HashMap; + +fn evaluated_storage_value(slot: &EvaluationSlot) -> Option { + slot.value.as_ref().and_then(|value|match value { + EvaluatedValue::Int(value) => Some(StorageValue::Integer(*value)), + _ => None, + }) +} + +struct MethodExpressionResolver<'a> { + environment: &'a MethodEnvironment, + method: &'a DMLMethodRef, + object: &'a DMLCompositeObject, + types: &'a TypeDatabase, +} + +impl MethodExpressionResolver<'_> { + fn resolve_type_with( + &mut self, + typed: &DMLType, + context: &mut EvaluationContext, + report: &mut Vec, + ) -> (Vec, DMLConcreteType) { + eval_type_with_expression(typed, self.types, |expression| { + let evaluated = expression.evaluate_with(context, self, report); + match evaluated.slot().and_then(|slot|slot.typed.as_ref()) { + Some(DMLResolvedType::Actual(typed)) => Some(typed.clone()), + _ => None, + } + }) + } + + fn resolve_call(&self, call: &FunctionCall) -> Option> { + if let ExpressionKind::MemberLiteral(member) = call.function.as_ref() { + return self.object.resolved_method_calls + .get(&member.right.span).map(Arc::clone); + } + let ExpressionKind::Identifier(identifier) = call.function.as_ref() + else { return None; }; + if identifier.name.val == "default" { + return self.method.get_default() + .and_then(DefaultCallReference::as_valid) + .map(Arc::clone); + } + self.object.get_method(identifier.name.val.as_str()) + .or_else(|| { + let lexical_trait = self.method.template_ref.as_ref()?; + let implementation = lookup_impl( + lexical_trait, &identifier.name.val)?; + let concrete = implementation.get_defined_method( + &identifier.name.val)?; + Some(Arc::new(DMLMethodRef { + template_ref: Some(Arc::clone(implementation)), + concrete_decl: concrete.clone(), + })) + }) + } + + fn check_call_arguments( + &mut self, + call: &FunctionCall, + called: &DMLMethodRef, + context: &mut EvaluationContext, + report: &mut Vec, + ) { + if call.arguments.len() != called.args().len() { + report.push(DMLError { + span: call.span, + description: format!( + "Method '{}' expects {} arguments but call provides {}", + called.identity(), called.args().len(), call.arguments.len()), + related: vec![(*called.location(), + "Method declared here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + for (initializer, argument) in call.arguments.iter().zip(called.args()) { + let (SingleInitializer::Expression(expression), + DMLMethodArg::Typed(declaration)) = (initializer, argument) + else { + continue; + }; + let DMLResolvedType::Actual(target) = &declaration.type_ref else { + continue; + }; + let evaluated = expression.evaluate_with(context, self, report); + let Some(slot) = evaluated.slot() else { + continue; + }; + let Some(DMLResolvedType::Actual(source)) = &slot.typed else { + continue; + }; + let value = evaluated_storage_value(slot); + let compatibility = check_storage_compatibility( + target, source, value); + let target_name = target.description(self.types); + let source_name = source.description(self.types); + let description = match compatibility { + StorageCompatibility::Incompatible => format!( + "Cannot pass value of type '{}' to argument '{}' of method \ + '{}' with type '{}'", + source_name, declaration.name.val, called.identity(), + target_name), + StorageCompatibility::Truncates(truncation) => format!( + "Value of type '{}' passed to argument '{}' of method '{}' \ + may be truncated when stored in type '{}'{}", + source_name, declaration.name.val, called.identity(), + target_name, truncation.target_suffix()), + StorageCompatibility::Compatible => continue, + }; + let error = DMLError { + span: *expression.span(), + description, + related: vec![(declaration.name.span, + "Argument declared here".to_string())], + severity: Some(match compatibility { + StorageCompatibility::Incompatible => + DiagnosticSeverity::ERROR, + _ => DiagnosticSeverity::WARNING, + }), + }; + match compatibility { + StorageCompatibility::Truncates(_) => if false { + report.push(error); + }, + _ => report.push(error), + } + } + } +} + +impl ExpressionResolver for MethodExpressionResolver<'_> { + fn identifier_type(&mut self, name: &str) -> Option { + self.environment.get(name) + .and_then(|binding|binding.typed.clone()) + .map(DMLResolvedType::Actual) + } + + fn resolve_type( + &mut self, + typed: &DMLType, + context: &mut EvaluationContext, + report: &mut Vec, + ) -> Option { + let (anonymous, resolved) = self.resolve_type_with( + typed, context, report); + anonymous.is_empty().then_some(DMLResolvedType::Actual(resolved)) + } + + fn function_call( + &mut self, + call: &FunctionCall, + context: &mut EvaluationContext, + report: &mut Vec, + ) -> EvaluationResult { + let Some(called) = self.resolve_call(call) else { + return EvaluationResult::UnknownArity; + }; + self.check_call_arguments(call, &called, context, report); + let slots = called.returns().iter().cloned() + .map(EvaluationSlot::from_type).collect(); + EvaluationResult::Known { + slots, + origin: Some(EvaluationOrigin { + identity: called.identity().to_string(), + location: *called.location(), + }), + } + } +} + +fn check_local_variable_types(statement: &Statement, + arguments: &[DMLMethodArg], + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec) { + struct ExpressionStorage { + source: DMLConcreteType, + value: Option, + compatibility: StorageCompatibility, + } + + struct ReturnSource { + span: ZeroSpan, + slot: EvaluationSlot, + } + + fn assignment_binding<'a>( + identifier: &crate::analysis::structure::expressions::Identifier, + environment: &'a MethodEnvironment, + report: &mut Vec, + ) -> Option<&'a MethodBinding> { + let binding = environment.get(&identifier.name.val)?; + if !binding.writable { + report.push(DMLError { + span: identifier.name.span, + description: format!( + "Cannot assign to inline method argument '{}'", + identifier.name.val), + related: vec![(binding.declaration_span, + "Inline argument declared here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + return None; + } + Some(binding) + } + + fn evaluate_expression( + expression: &ExpressionKind, + environment: &MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec, + ) -> EvaluationResult { + let mut context = EvaluationContext::new(); + let mut resolver = MethodExpressionResolver { + environment, method, object, types, + }; + expression.evaluate_with(&mut context, &mut resolver, report) + } + + fn evaluate_type( + typed: &DMLType, + environment: &MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec, + ) -> (Vec, DMLConcreteType) { + let mut context = EvaluationContext::new(); + let mut resolver = MethodExpressionResolver { + environment, method, object, types, + }; + resolver.resolve_type_with(typed, &mut context, report) + } + + fn slot_storage(slot: &EvaluationSlot) + -> Option<(DMLConcreteType, Option)> { + let Some(DMLResolvedType::Actual(source)) = &slot.typed else { + return None; + }; + Some((source.clone(), evaluated_storage_value(slot))) + } + + fn check_expression_storage( + target: &DMLConcreteType, + expression: &ExpressionKind, + environment: &MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec, + ) -> Option { + let evaluated = evaluate_expression( + expression, environment, method, object, types, report); + let (source, value) = slot_storage(evaluated.slot()?)?; + let compatibility = check_storage_compatibility( + target, &source, value); + Some(ExpressionStorage { source, value, compatibility }) + } + + fn expression_sources( + expression: &ExpressionKind, + object: &DMLCompositeObject, + method: &DMLMethodRef, + environment: &MethodEnvironment, + types: &TypeDatabase, + report: &mut Vec, + ) -> Option> { + match evaluate_expression( + expression, environment, method, object, types, report) + { + EvaluationResult::Known { slots, .. } => Some(slots.into_iter() + .map(|slot|ReturnSource { + span: *expression.span(), slot, + }).collect()), + EvaluationResult::UnknownArity => None, + } + } + + fn check_return( + return_statement: &crate::analysis::structure::statements::Return, + method: &DMLMethodRef, + object: &DMLCompositeObject, + environment: &MethodEnvironment, + types: &TypeDatabase, + report: &mut Vec, + ) { + let sources = if let Some(initializer) = &return_statement.ret { + match initializer { + Initializer::Single(SingleInitializer::Expression(expression)) => { + let Some(sources) = expression_sources( + expression, object, method, environment, types, report) + else { + return; + }; + sources + }, + Initializer::Tuple(tuple) => { + let mut sources = vec![]; + for initializer in &tuple.list { + match initializer { + SingleInitializer::Expression(expression) => { + let Some(mut inferred) = expression_sources( + expression, object, method, + environment, types, report) + else { + return; + }; + sources.append(&mut inferred); + }, + _ => sources.push(ReturnSource { + span: *initializer.span(), + slot: EvaluationSlot::unknown(), + }), + } + } + sources + }, + Initializer::Single(initializer) => + vec![ReturnSource { + span: *initializer.span(), + slot: EvaluationSlot::unknown(), + }], + } + } else { + vec![] + }; + let declaration = method.get_decl(); + if sources.len() != declaration.return_types.len() { + report.push(DMLError { + span: return_statement.span, + description: format!( + "Method '{}' declares {} return values but return statement \ + provides {}", + method.identity(), declaration.return_types.len(), + sources.len()), + related: vec![(declaration.name.span, + "Method declared here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + for (source, target) in + sources.into_iter().zip(&declaration.return_types) + { + let Some((source_type, value)) = slot_storage(&source.slot) else { + continue; + }; + let DMLResolvedType::Actual(target) = target else { + continue; + }; + let compatibility = check_storage_compatibility( + target, &source_type, value); + let target_name = target.description(types); + let source_name = source_type.description(types); + let description = match compatibility { + StorageCompatibility::Incompatible => format!( + "Cannot return value of type '{}' from method '{}' with \ + return type '{}'", + source_name, method.identity(), target_name), + StorageCompatibility::Truncates(truncation) => format!( + "Value of type '{}' returned from method '{}' may be \ + truncated when stored in return type '{}'{}", + source_name, method.identity(), target_name, + truncation.target_suffix()), + StorageCompatibility::Compatible => continue, + }; + let error = DMLError { + span: source.span, + description, + related: vec![(*target.span(), + "Return type declared here".to_string())], + severity: Some(match compatibility { + StorageCompatibility::Incompatible => + DiagnosticSeverity::ERROR, + _ => DiagnosticSeverity::WARNING, + }), + }; + match compatibility { + StorageCompatibility::Truncates(_) => if false { + report.push(error); + }, + _ => report.push(error), + } + } + } + + fn storage_diagnostic( + target: &DMLConcreteType, + expression: &ExpressionKind, + storage: ExpressionStorage, + variable_name: &str, + declaration_span: ZeroSpan, + initializer: bool, + types: &TypeDatabase, + ) -> Option { + let target_name = target.description(types); + let source_name = storage.source.description(types); + let related = vec![(declaration_span, + format!("Local variable '{}' declared as '{}' here", + variable_name, target_name))]; + + match (storage.compatibility, storage.value) { + (StorageCompatibility::Incompatible, _) => Some(DMLError { + span: *expression.span(), + description: if initializer { + format!("Cannot initialize local variable '{}' of type '{}' \ + with value of type '{}'", + variable_name, target_name, source_name) + } else { + format!("Cannot assign value of type '{}' to local variable \ + '{}' of type '{}'", + source_name, variable_name, target_name) + }, + related, + severity: Some(DiagnosticSeverity::ERROR), + }), + (StorageCompatibility::Truncates(truncation), Some(_)) => { + if false { + Some(DMLError { + span: *expression.span(), + description: if initializer { + format!("Initializer for local variable '{}' does not fit in \ + type '{}'{} and will be truncated", + variable_name, target_name, + truncation.target_suffix()) + } else { + format!("Value assigned to local variable '{}' does not fit in \ + type '{}'{} and will be truncated", + variable_name, target_name, + truncation.target_suffix()) + }, + related, + severity: Some(DiagnosticSeverity::WARNING), + }) + } else { + None + } + }, + (StorageCompatibility::Truncates(truncation), None) => + if false { + Some(DMLError { + span: *expression.span(), + description: if initializer { + format!("Initializer for local variable '{}' of type '{}' \ + may be truncated when stored in type '{}'{}", + variable_name, source_name, target_name, + truncation.target_suffix()) + } else { + format!("Assignment to local variable '{}' from type '{}' \ + may be truncated when stored in type '{}'{}", + variable_name, source_name, target_name, + truncation.target_suffix()) + }, + related, + severity: Some(DiagnosticSeverity::WARNING), + }) + } else { + None + }, + (StorageCompatibility::Compatible, _) => None, + } + } + + fn return_binding_diagnostic( + target: &DMLConcreteType, + source: &DMLConcreteType, + expression: &ExpressionKind, + origin: &EvaluationOrigin, + variable_name: &str, + declaration_span: ZeroSpan, + initializer: bool, + types: &TypeDatabase, + ) -> Option { + let compatibility = check_storage_compatibility(target, source, None); + let target_name = target.description(types); + let source_name = source.description(types); + let description = match compatibility { + StorageCompatibility::Incompatible => if initializer { + format!("Cannot initialize local variable '{}' of type '{}' with \ + return value of type '{}' from method '{}'", + variable_name, target_name, source_name, + origin.identity) + } else { + format!("Cannot assign return value of type '{}' from method '{}' \ + to local variable '{}' of type '{}'", + source_name, origin.identity, variable_name, + target_name) + }, + StorageCompatibility::Truncates(truncation) => if initializer { + format!("Return value of type '{}' from method '{}' may be \ + truncated when stored in local variable '{}' of type \ + '{}'{}", + source_name, origin.identity, variable_name, + target_name, truncation.target_suffix()) + } else { + format!("Return value of type '{}' from method '{}' may be \ + truncated when assigned to local variable '{}' of type \ + '{}'{}", + source_name, origin.identity, variable_name, + target_name, truncation.target_suffix()) + }, + StorageCompatibility::Compatible => return None, + }; + let error = DMLError { + span: *expression.span(), + description, + related: vec![(declaration_span, + format!("Local variable '{}' declared here", variable_name))], + severity: Some(match compatibility { + StorageCompatibility::Incompatible => DiagnosticSeverity::ERROR, + _ => DiagnosticSeverity::WARNING, + }), + }; + match compatibility { + StorageCompatibility::Truncates(_) => if false { + Some(error) + } else { + None + }, + _ => Some(error), + } + } + + fn array_storage_diagnostic( + element_type: &DMLConcreteType, + array_type: &DMLConcreteType, + expression: &ExpressionKind, + storage: ExpressionStorage, + variable_name: &str, + element_index: usize, + declaration_span: ZeroSpan, + types: &TypeDatabase, + ) -> Option { + let element_name = element_type.description(types); + let source_name = storage.source.description(types); + let related = vec![(declaration_span, + format!("Local array '{}' declared as '{}' here", + variable_name, array_type.description(types)))]; + let description = match (storage.compatibility, storage.value) { + (StorageCompatibility::Incompatible, _) => format!( + "Cannot initialize element {} of local array '{}' of type '{}' \ + with value of type '{}'", + element_index, variable_name, element_name, source_name), + (StorageCompatibility::Truncates(truncation), Some(_)) => format!( + "Initializer for element {} of local array '{}' does not fit in \ + type '{}'{} and will be truncated", + element_index, variable_name, element_name, + truncation.target_suffix()), + (StorageCompatibility::Truncates(truncation), None) => format!( + "Initializer for element {} of local array '{}' of type '{}' may \ + be truncated when stored in type '{}'{}", + element_index, variable_name, source_name, element_name, + truncation.target_suffix()), + (StorageCompatibility::Compatible, _) => return None, + }; + let error = DMLError { + span: *expression.span(), + description, + related, + severity: Some(match storage.compatibility { + StorageCompatibility::Incompatible => DiagnosticSeverity::ERROR, + _ => DiagnosticSeverity::WARNING, + }), + }; + match storage.compatibility { + StorageCompatibility::Truncates(_) => if false { + Some(error) + } else { + None + }, + _ => Some(error), + } + } + + fn check_variable(variable: &crate::analysis::structure::objects::Variable, + environment: &mut MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec) { + let call_binding = if let [initializer] = variable.values.as_slice() { + initializer.expression().and_then(|expression| { + if !matches!(expression.as_ref(), ExpressionKind::FunctionCall(_)) { + return None; + } + match evaluate_expression( + expression, environment, method, object, types, report) + { + EvaluationResult::Known { + slots, + origin: Some(origin), + } => Some((expression, origin, slots)), + _ => None, + } + }) + } else { + None + }; + if let Some((expression, origin, slots)) = &call_binding { + if slots.len() != variable.vars.len() { + report.push(DMLError { + span: *expression.span(), + description: format!( + "Method '{}' returns {} values but declaration binds {}", + origin.identity, slots.len(), variable.vars.len()), + related: vec![(origin.location, + "Method declared here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } + for (index, declaration) in variable.vars.iter().enumerate() { + if declaration.kind != VariableDeclKind::Local { + continue; + } + types.report_unknown_types(&declaration.typed, report); + let (anonymous, target) = evaluate_type( + &declaration.typed, environment, method, object, types, report); + if anonymous.is_empty() { + if let Some((expression, origin, slots)) = &call_binding { + if let Some(source) = slots.get(index).and_then(|slot| + match &slot.typed { + Some(DMLResolvedType::Actual(source)) => Some(source), + _ => None, + }) + { + if let Some(error) = return_binding_diagnostic( + &target, source, expression, origin, + &declaration.object.name.val, + declaration.object.name.span, true, types) + { + report.push(error); + } + } + } else if let Some(initializer) = variable.values.get(index) { + let variable_name = &declaration.object.name.val; + if let Some(expression) = initializer.expression() { + if let Some(storage) = check_expression_storage( + &target, expression, environment, method, object, + types, report) + { + if let Some(error) = storage_diagnostic( + &target, expression, storage, variable_name, + declaration.object.name.span, true, types) + { + report.push(error); + } + } + } else if let (Some(element_type), Some(elements)) = + (target.array_element(), initializer.list()) + { + if let Some(length) = target.array_length() { + if length != elements.len() as u64 { + report.push(DMLError { + span: *initializer.span(), + description: format!( + "Local array '{}' has length {} but its \ + initializer has {} elements", + variable_name, length, elements.len()), + related: vec![(declaration.object.name.span, + format!("Local array '{}' declared here", + variable_name))], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } + for (element_index, initializer) in + elements.iter().enumerate() + { + let Some(expression) = initializer.expression() else { + continue; + }; + let Some(storage) = check_expression_storage( + element_type, expression, environment, method, + object, types, report) + else { + continue; + }; + if let Some(error) = array_storage_diagnostic( + element_type, &target, expression, storage, + variable_name, element_index, + declaration.object.name.span, types) + { + report.push(error); + } + } + } + } + environment.insert( + declaration.object.name.val.clone(), + MethodBinding { + typed: Some(target), + declaration_span: declaration.object.name.span, + writable: true, + }); + } + } + } + + fn check_assignment( + assignment: &crate::analysis::structure::statements::AssignStatement, + environment: &MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec, + ) { + fn check_pair( + assignee: &ExpressionKind, + expression: &ExpressionKind, + environment: &MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec, + ) { + let ExpressionKind::Identifier(identifier) = assignee else { + return; + }; + let Some(binding) = assignment_binding( + identifier, environment, report) + else { + return; + }; + let Some(target) = binding.typed.as_ref() else { + return; + }; + let Some(storage) = check_expression_storage( + target, expression, environment, method, object, types, report) + else { + return; + }; + if let Some(error) = storage_diagnostic( + target, expression, storage, &identifier.name.val, + binding.declaration_span, false, types) + { + report.push(error); + } + } + + if let Assigner::Chain(expressions) = &assignment.assigner { + if let [expression] = expressions.as_slice() { + let call_binding = matches!( + expression.as_ref(), ExpressionKind::FunctionCall(_)) + .then(||evaluate_expression( + expression, environment, method, object, types, report)); + if let Some(EvaluationResult::Known { + slots, + origin: Some(origin), + }) = call_binding { + if assignment.assignees.len() != slots.len() { + report.push(DMLError { + span: *expression.span(), + description: format!( + "Method '{}' returns {} values but assignment \ + has {} targets", + origin.identity, slots.len(), + assignment.assignees.len()), + related: vec![(origin.location, + "Method declared here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + for (assignee, source) in + assignment.assignees.iter().zip(&slots) + { + let ExpressionKind::Identifier(identifier) = + assignee.as_ref() + else { + continue; + }; + let Some(binding) = assignment_binding( + identifier, environment, report) + else { + continue; + }; + let Some(target) = binding.typed.as_ref() else { + continue; + }; + let Some(DMLResolvedType::Actual(source)) = + source.typed.as_ref() + else { + continue; + }; + if let Some(error) = return_binding_diagnostic( + target, source, expression, &origin, + &identifier.name.val, binding.declaration_span, + false, types) + { + report.push(error); + } + } + return; + } + } + } + + match &assignment.assigner { + Assigner::Initializer(Initializer::Single( + SingleInitializer::Expression(expression))) => { + if let [assignee] = assignment.assignees.as_slice() { + check_pair(assignee, expression, environment, method, + object, types, report); + } + }, + Assigner::Initializer(Initializer::Tuple(tuple)) => { + if assignment.assignees.len() != tuple.list.len() { + report.push(DMLError { + span: assignment.span, + description: format!( + "Simultaneous assignment has {} targets but \ + initializer provides {} values", + assignment.assignees.len(), tuple.list.len()), + related: vec![], + severity: Some(DiagnosticSeverity::ERROR), + }); + return; + } + for (assignee, initializer) in + assignment.assignees.iter().zip(&tuple.list) + { + if let SingleInitializer::Expression(expression) = initializer { + check_pair( + assignee, expression, environment, method, object, + types, report); + } + } + }, + Assigner::Chain(expressions) => { + let Some((last, targets)) = expressions.split_last() else { + return; + }; + let evaluated = evaluate_expression( + last, environment, method, object, types, report); + if assignment.assignees.len() > 1 && targets.is_empty() + && evaluated.slot().is_some() + { + report.push(DMLError { + span: assignment.span, + description: format!( + "Simultaneous assignment has {} targets but \ + initializer provides 1 value", + assignment.assignees.len()), + related: vec![], + severity: Some(DiagnosticSeverity::ERROR), + }); + return; + } + let Some((mut source, mut value)) = evaluated.slot() + .and_then(slot_storage) + else { + return; + }; + let mut source_expression = last.as_ref(); + for target_expression in targets.iter().rev() + .chain(assignment.assignees.iter().rev()) + { + let ExpressionKind::Identifier(identifier) = + target_expression.as_ref() + else { + return; + }; + let Some(binding) = assignment_binding( + identifier, environment, report) + else { + return; + }; + let Some(target) = binding.typed.as_ref() else { + return; + }; + let compatibility = check_storage_compatibility( + target, &source, value); + let storage = ExpressionStorage { + source: source.clone(), value, compatibility, + }; + if let Some(error) = storage_diagnostic( + target, source_expression, storage, + &identifier.name.val, binding.declaration_span, + false, types) + { + report.push(error); + } + source = target.clone(); + value = None; + source_expression = target_expression; + } + }, + _ => (), + } + } + + fn check_switch_cases(cases: &[SwitchCase], + environment: &mut MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec) { + for case in cases { + match case { + SwitchCase::Statement(statement) => + check_statement(statement, environment, + method, object, types, report), + SwitchCase::HashIf(hash_if) => { + let mut true_environment = environment.clone(); + check_switch_cases(&hash_if.truecases, &mut true_environment, + method, object, types, report); + let mut false_environment = environment.clone(); + check_switch_cases(&hash_if.falsecases, &mut false_environment, + method, object, types, report); + }, + _ => (), + } + } + } + + fn check_statement(statement: &Statement, + environment: &mut MethodEnvironment, + method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + report: &mut Vec) { + match statement.as_ref() { + StatementKind::Compound(compound) => { + let mut block_environment = environment.clone(); + for statement in &compound.statements { + check_statement(statement, &mut block_environment, + method, object, types, report); + } + }, + StatementKind::VariableDecl(variable) => + check_variable( + variable, environment, method, object, types, report), + StatementKind::Assign(assignment) => + check_assignment(assignment, environment, method, + object, types, report), + StatementKind::Return(return_statement) => + check_return(return_statement, method, object, + environment, types, report), + StatementKind::Expression(statement) => { + let _ = evaluate_expression( + &statement.expression, environment, method, object, + types, report); + }, + StatementKind::Switch(statement) => + check_switch_cases(&statement.cases, environment, + method, object, types, report), + StatementKind::If(statement) => { + let mut branch_environment = environment.clone(); + check_statement(&statement.ifbody, &mut branch_environment, + method, object, types, report); + if let Some(elsebody) = &statement.elsebody { + let mut branch_environment = environment.clone(); + check_statement(elsebody, &mut branch_environment, + method, object, types, report); + } + }, + StatementKind::HashIf(statement) => { + let mut branch_environment = environment.clone(); + check_statement(&statement.ifbody, &mut branch_environment, + method, object, types, report); + if let Some(elsebody) = &statement.elsebody { + let mut branch_environment = environment.clone(); + check_statement(elsebody, &mut branch_environment, + method, object, types, report); + } + }, + StatementKind::While(statement) => { + let mut body_environment = environment.clone(); + check_statement(&statement.body, &mut body_environment, + method, object, types, report); + }, + StatementKind::DoWhile(statement) => { + let mut body_environment = environment.clone(); + check_statement(&statement.body, &mut body_environment, + method, object, types, report); + }, + StatementKind::For(statement) => { + let mut loop_environment = environment.clone(); + if let Some(ForPre::Declaration(variable)) = &statement.pre { + check_variable(variable, &mut loop_environment, + method, object, types, report); + } + check_statement(&statement.body, &mut loop_environment, + method, object, types, report); + }, + StatementKind::ForEach(statement) => { + let mut body_environment = environment.clone(); + check_statement(&statement.body, &mut body_environment, + method, object, types, report); + }, + StatementKind::TryCatch(statement) => { + let mut try_environment = environment.clone(); + check_statement(&statement.tryblock, &mut try_environment, + method, object, types, report); + let mut catch_environment = environment.clone(); + check_statement(&statement.catchblock, &mut catch_environment, + method, object, types, report); + }, + StatementKind::HashSelect(statement) => { + let mut select_environment = environment.clone(); + check_statement(&statement.selectbranch, &mut select_environment, + method, object, types, report); + let mut else_environment = environment.clone(); + check_statement(&statement.elsebranch, &mut else_environment, + method, object, types, report); + }, + _ => (), + } + } + + let mut environment = HashMap::new(); + for argument in arguments { + match argument { + DMLMethodArg::Typed(declaration) => { + environment.insert( + declaration.name.val.clone(), MethodBinding { + typed: match &declaration.type_ref { + DMLResolvedType::Actual(resolved) => + Some(resolved.clone()), + DMLResolvedType::Dummy(_) => None, + }, + declaration_span: declaration.name.span, + writable: true, + }); + }, + DMLMethodArg::Inline(name) => { + environment.insert(name.val.clone(), MethodBinding { + typed: None, + declaration_span: name.span, + writable: false, + }); + }, + } + } + check_statement( + statement, &mut environment, method, object, types, report); +} + +pub fn check_method_body_types(method: &DMLMethodRef, + object: &DMLCompositeObject, + types: &TypeDatabase, + live: bool, + report: &mut Vec) { + let declaration = method.get_decl(); + if live { + report_error_statements(&declaration.body, report); + } + check_local_variable_types( + &declaration.body, &declaration.method_args, + method, object, types, report); +} + +// Mirrors DMLC's EERRSTMT; only '#if' (not 'if') can exclude an error, and +// '#if' branches are only checked when the condition is provably known +fn report_error_statements(statement: &Statement, + report: &mut Vec) { + fn known_condition(condition: &ExpressionKind) -> Option { + condition.evaluate(&mut EvaluationContext::new(), &mut vec![]) + .as_bool() + } + fn case_statements<'s>(cases: &'s [SwitchCase], + statements: &mut Vec<&'s Statement>) { + for case in cases { + match case { + SwitchCase::Statement(statement) => statements.push(statement), + SwitchCase::HashIf(hashif) => + match known_condition(&hashif.condition) { + Some(true) => + case_statements(&hashif.truecases, statements), + Some(false) => + case_statements(&hashif.falsecases, statements), + None => (), + }, + SwitchCase::Case(_) | SwitchCase::Default(_) => (), + } + } + } + let children: Vec<&Statement> = match statement.as_ref() { + StatementKind::Error(error) => { + report.push(DMLError { + span: error.span, + description: match error.message.as_deref() { + Some(ExpressionKind::StringLiteral(message)) => + message.val.clone(), + _ => "forced compilation error in source code" + .to_string(), + }, + related: vec![], + severity: Some(DiagnosticSeverity::ERROR), + }); + vec![] + }, + StatementKind::Compound(compound) => + compound.statements.iter().collect(), + StatementKind::If(statement) => std::iter::once(&statement.ifbody) + .chain(statement.elsebody.as_ref()).collect(), + StatementKind::HashIf(statement) => + match known_condition(&statement.condition) { + Some(true) => vec![&statement.ifbody], + Some(false) => statement.elsebody.iter().collect(), + None => vec![], + }, + StatementKind::While(statement) => vec![&statement.body], + StatementKind::DoWhile(statement) => vec![&statement.body], + StatementKind::For(statement) => vec![&statement.body], + StatementKind::ForEach(statement) => vec![&statement.body], + StatementKind::TryCatch(statement) => + vec![&statement.tryblock, &statement.catchblock], + StatementKind::HashSelect(statement) => + vec![&statement.selectbranch, &statement.elsebranch], + StatementKind::Switch(statement) => { + let mut statements = vec![]; + case_statements(&statement.cases, &mut statements); + statements + }, + _ => vec![], + }; + for child in children { + report_error_statements(child, report); + } +} + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct MethodDecl { pub name: DMLString, pub modifier: MethodModifier, pub independent: bool, + pub startup: bool, + pub memoized: bool, pub default: bool, pub throws: bool, pub method_args: Vec, pub return_types: Vec, pub body: Statement, + // Whether the body references 'default', i.e. may run the overridden method + pub calls_default: bool, pub span: ZeroSpan, } @@ -165,6 +1326,8 @@ pub trait MethodDeclaration : DMLNamedMember + MaybeAbstract { fn is_shared(&self) -> bool; fn is_default(&self) -> bool; fn throws(&self) -> bool; + // (qualifier name, whether present) for independent, startup and memoized + fn qualifiers(&self) -> [(&'static str, bool); 3]; fn fully_typed(&self) -> bool { for arg in self.args() { if matches!(arg, DMLMethodArg::Inline(_)) { @@ -217,6 +1380,21 @@ pub trait MethodDeclaration : DMLNamedMember + MaybeAbstract { severity: Some(DiagnosticSeverity::ERROR), }); } + for ((qualifier, mine), (_, theirs)) in + self.qualifiers().iter().zip(overridden.qualifiers().iter()) { + if mine != theirs { + report.push(DMLError { + span: *self.location(), + description: format!( + "One declaration is qualified as {}, but the other \ + is not", qualifier), + related: vec![(*overridden.location(), + "Overridden method declared here" + .to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } if self.args().len() != overridden.args().len() { report.push(DMLError { span: *self.location(), @@ -253,7 +1431,7 @@ pub trait MethodDeclaration : DMLNamedMember + MaybeAbstract { }); } for (type1, type2) in self.returns().iter().zip(overridden.returns()) { - if !type1.equivalent(type2) { + if !type1.equivalent_unqualified(type2) { report.push(DMLError { span: *type1.span(), description: "Mismatching return type in \ @@ -281,6 +1459,12 @@ impl MethodDeclaration for MethodDecl { self.throws } + fn qualifiers(&self) -> [(&'static str, bool); 3] { + [("independent", self.independent), + ("startup", self.startup), + ("memoized", self.memoized)] + } + fn args(&self) -> &Vec { &self.method_args } @@ -291,17 +1475,26 @@ impl MethodDeclaration for MethodDecl { } impl MethodDecl { - pub fn from_content(content: &Method, report: &mut Vec) + pub fn from_content(content: &Method, types: &TypeDatabase, + report: &mut Vec) -> MethodDecl { MethodDecl { name: content.object.name.clone(), modifier: content.modifier, independent: content.independent, + startup: content.startup, + memoized: content.memoized, default: content.default, body: content.body.clone(), + calls_default: content.references.iter() + .filter_map(Reference::as_code_ref) + .filter_map(|reference|reference.as_variable_ref()) + .any(|variable|matches!(&variable.reference, + NodeRef::Simple(name) + if name.val == "default")), throws: content.throws, - method_args: eval_method_args(&content.arguments, report), - return_types: eval_method_returns(&content.returns, report), + method_args: eval_method_args(&content.arguments, types, report), + return_types: eval_method_returns(&content.returns, types, report), span: *content.span(), } } @@ -368,6 +1561,10 @@ impl MethodDeclaration for DMLMethodRef { self.concrete_decl.throws() } + fn qualifiers(&self) -> [(&'static str, bool); 3] { + self.concrete_decl.qualifiers() + } + fn args(&self) -> &Vec { self.concrete_decl.args() } @@ -506,6 +1703,10 @@ impl MethodDeclaration for DMLConcreteMethod { self.decl.throws() } + fn qualifiers(&self) -> [(&'static str, bool); 3] { + self.decl.qualifiers() + } + fn args(&self) -> &Vec { self.decl.args() } @@ -526,3 +1727,288 @@ impl DeclarationSpan for DMLConcreteMethod { self.decl.span() } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::analysis::structure::types::{DMLType, DMLTypeKind}; + use crate::analysis::templating::types::{DMLConcreteType, + DMLConcreteTypeKind}; + + struct TestMethod { + location: ZeroSpan, + arguments: Vec, + returns: Vec, + } + + impl DMLNamedMember for TestMethod { + fn identity(&self) -> &str { "test_method" } + fn kind_name(&self) -> &'static str { "method" } + fn location(&self) -> &ZeroSpan { &self.location } + } + + impl MaybeAbstract for TestMethod { + fn is_abstract(&self) -> bool { false } + } + + impl MethodDeclaration for TestMethod { + fn is_shared(&self) -> bool { false } + fn is_default(&self) -> bool { false } + fn throws(&self) -> bool { false } + fn qualifiers(&self) -> [(&'static str, bool); 3] { + [("independent", false), ("startup", false), ("memoized", false)] + } + fn args(&self) -> &Vec { &self.arguments } + fn returns(&self) -> &Vec { &self.returns } + } + + fn typed_argument(name: &str, type_name: &str) -> DMLMethodArg { + let span = ZeroSpan::invalid("method-type-test.dml"); + let type_syntax = DMLType { + kind: DMLTypeKind::Named(DMLString { + val: type_name.to_string(), + span, + }), + span, + }; + DMLMethodArg::Typed(Declaration { + name: DMLString { val: name.to_string(), span }, + type_ref: DMLConcreteType { + kind: DMLConcreteTypeKind::Named(type_name.to_string()), + span, + }.into(), + type_syntax, + }) + } + + fn unresolved_argument(name: &str) -> DMLMethodArg { + let span = ZeroSpan::invalid("method-type-test.dml"); + DMLMethodArg::Typed(Declaration { + name: DMLString { val: name.to_string(), span }, + type_ref: DMLResolvedType::Dummy(span), + type_syntax: DMLType { + kind: DMLTypeKind::Unknown, + span, + }, + }) + } + + fn wrapped_argument(name: &str, + kind: impl FnOnce(Box) + -> DMLConcreteTypeKind, + inner: DMLConcreteType) -> DMLMethodArg { + let span = ZeroSpan::invalid("method-type-test.dml"); + DMLMethodArg::Typed(Declaration { + name: DMLString { val: name.to_string(), span }, + type_ref: DMLConcreteType { + kind: kind(Box::new(inner)), + span, + }.into(), + type_syntax: DMLType { + kind: DMLTypeKind::Unknown, + span, + }, + }) + } + + fn concrete_named_type(name: &str) -> DMLConcreteType { + DMLConcreteType { + kind: DMLConcreteTypeKind::Named(name.to_string()), + span: ZeroSpan::invalid("method-type-test.dml"), + } + } + + #[test] + fn mismatching_argument_types_are_reported() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let method = TestMethod { + location, + arguments: vec![typed_argument("value", "int")], + returns: vec![], + }; + let overridden = TestMethod { + location, + arguments: vec![typed_argument("value", "packet_t")], + returns: vec![], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert!(report.iter().any(|error| + error.description == "Mismatching argument type in method override")); + } + + #[test] + fn matching_argument_types_are_accepted() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let method = TestMethod { + location, + arguments: vec![typed_argument("value", "int")], + returns: vec![], + }; + let overridden = TestMethod { + location, + arguments: vec![typed_argument("value", "int")], + returns: vec![], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert!(report.is_empty()); + } + + #[test] + fn numeric_conversion_argument_types_are_reported() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let method = TestMethod { + location, + arguments: vec![typed_argument("value", "float")], + returns: vec![], + }; + let overridden = TestMethod { + location, + arguments: vec![typed_argument("value", "int")], + returns: vec![], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert!(report.iter().any(|error| + error.description == "Mismatching argument type in method override")); + } + + #[test] + fn override_argument_compatibility_ignores_pointee_const_by_default() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let int_type = concrete_named_type("int"); + let const_int_type = DMLConcreteType { + kind: DMLConcreteTypeKind::Const(Box::new(int_type.clone())), + span: location, + }; + let int_pointer = wrapped_argument( + "value", DMLConcreteTypeKind::Pointer, int_type); + let const_int_pointer = wrapped_argument( + "value", DMLConcreteTypeKind::Pointer, const_int_type); + let accepting_override = TestMethod { + location, + arguments: vec![const_int_pointer.clone()], + returns: vec![], + }; + let base = TestMethod { + location, + arguments: vec![int_pointer.clone()], + returns: vec![], + }; + let rejecting_override = TestMethod { + location, + arguments: vec![int_pointer], + returns: vec![], + }; + let const_base = TestMethod { + location, + arguments: vec![const_int_pointer], + returns: vec![], + }; + let mut accepted_report = vec![]; + let mut rejected_report = vec![]; + + accepting_override.check_override(&base, &mut accepted_report); + rejecting_override.check_override(&const_base, &mut rejected_report); + + assert!(accepted_report.is_empty()); + assert!(rejected_report.is_empty()); + } + + #[test] + fn override_argument_compatibility_ignores_top_level_const() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let const_int = wrapped_argument( + "value", DMLConcreteTypeKind::Const, concrete_named_type("int")); + let method = TestMethod { + location, + arguments: vec![const_int], + returns: vec![], + }; + let overridden = TestMethod { + location, + arguments: vec![typed_argument("value", "int")], + returns: vec![], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert!(report.is_empty()); + } + + #[test] + fn override_return_types_remain_exact() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let method = TestMethod { + location, + arguments: vec![], + returns: vec![concrete_named_type("float").into()], + }; + let overridden = TestMethod { + location, + arguments: vec![], + returns: vec![concrete_named_type("int").into()], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert!(report.iter().any(|error| + error.description == "Mismatching return type in method override")); + } + + #[test] + fn partially_known_signatures_compare_known_slots() { + let location = ZeroSpan::invalid("method-type-test.dml"); + let method = TestMethod { + location, + arguments: vec![ + unresolved_argument("unknown"), + typed_argument("known", "int"), + ], + returns: vec![ + DMLResolvedType::Dummy(location), + concrete_named_type("int").into(), + ], + }; + let overridden = TestMethod { + location, + arguments: vec![ + typed_argument("unknown", "packet_t"), + typed_argument("known", "float"), + ], + returns: vec![ + concrete_named_type("packet_t").into(), + concrete_named_type("float").into(), + ], + }; + let mut report = vec![]; + + method.check_override(&overridden, &mut report); + + assert_eq!(report.iter().filter(|error| + error.description == "Mismatching argument type in method override") + .count(), 1); + assert_eq!(report.iter().filter(|error| + error.description == "Mismatching return type in method override") + .count(), 1); + } + + #[test] + fn unresolved_return_types_remain_present_in_call_slots() { + let span = ZeroSpan::invalid("method-type-test.dml"); + + let slot = EvaluationSlot::from_type(DMLResolvedType::Dummy(span)); + + assert!(matches!(slot.typed, Some(DMLResolvedType::Dummy(found)) + if found == span)); + } +} diff --git a/src/analysis/templating/mod.rs b/src/analysis/templating/mod.rs index 35928234..977e1f05 100644 --- a/src/analysis/templating/mod.rs +++ b/src/analysis/templating/mod.rs @@ -10,11 +10,13 @@ pub mod evaluation; use crate::analysis::templating::types::DMLResolvedType; use crate::analysis::structure::expressions::DMLString; use crate::analysis::structure::objects::MaybeAbstract; +use crate::analysis::structure::types::DMLType; use crate::analysis::DMLNamed; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Declaration { pub type_ref: DMLResolvedType, + pub type_syntax: DMLType, pub name: DMLString, } diff --git a/src/analysis/templating/objects.rs b/src/analysis/templating/objects.rs index 3bfe76c9..1d7caabd 100644 --- a/src/analysis/templating/objects.rs +++ b/src/analysis/templating/objects.rs @@ -20,8 +20,11 @@ use crate::analysis::structure::toplevel::{ExistCondition, ObjectDecl, StatementSpec, TopLevel}; use crate::analysis::symbols::{DMLSymbolKind}; use crate::analysis::templating::Declaration; -use crate::analysis::templating::types::eval_type; -use crate::analysis::templating::methods::{DMLConcreteMethod, DMLMethodRef, DefaultCallReference, MethodDecl, MethodDeclaration}; +use crate::analysis::templating::types::{eval_type, TypeDatabase}; +use crate::analysis::templating::methods::{check_method_body_types, + DMLConcreteMethod, DMLMethodRef, + DefaultCallReference, MethodDecl, + MethodDeclaration}; use crate::analysis::templating::topology::{InEachStruct, InferiorVariant, Rank, RankDesc, RankDescKind, RankMaker, topsort}; @@ -345,6 +348,7 @@ pub fn make_device<'t>(path: &CanonPath, container: &'t mut StructureContainer, rankmaker: &mut RankMaker, eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> &'t DMLCompositeObject { debug!("Creating a device for {:?}", path); // create the faux spec for the device toplevel, importing the device file @@ -400,6 +404,7 @@ pub fn make_device<'t>(path: &CanonPath, None, container, eval_context, + types, report); let device_obj = container.get(obj_key).unwrap(); trace!("Device components are: {:?}", device_obj.components); @@ -957,6 +962,7 @@ pub struct DMLCompositeObject { pub components: HashMap, // Used for goto-implementation on composite objects pub used_ineach_locs: Vec, + pub resolved_method_calls: HashMap>, } impl DMLCompositeObject { @@ -1229,21 +1235,23 @@ enum VariableKind { } fn add_sessions(obj: &mut DMLCompositeObject, - sessions: SessionMapping) { + sessions: SessionMapping, + types: &TypeDatabase) { for (_, (used, mut cond_decls)) in sessions { if used { let (_, _, (s, i)) = cond_decls.swap_remove(0); - handle_variable(obj, s, i, VariableKind::Session) + handle_variable(obj, s, i, VariableKind::Session, types) } } } fn add_saveds(obj: &mut DMLCompositeObject, - saveds: SavedMapping) { + saveds: SavedMapping, + types: &TypeDatabase) { for (_, (used, mut cond_decls)) in saveds { if used { let (_, _, (s, i)) = cond_decls.swap_remove(0); - handle_variable(obj, s, i, VariableKind::Saved) + handle_variable(obj, s, i, VariableKind::Saved, types) } } } @@ -1294,7 +1302,13 @@ fn gather_parameters<'t>(obj_loc: &ZeroSpan, }, vec![]); // Add array index variable + let mut index_names = HashSet::new(); for arraydim in index_info { + // A repeated index variable is reported as a collision on its own + if arraydim.indexvar.val == "_" + || !index_names.insert(arraydim.indexvar.val.as_str()) { + continue; + } let new_decl = (make_objectdecl_auto_param( &arraydim.indexvar.val, &arraydim.indexvar.span, @@ -1495,13 +1509,40 @@ fn resolve_parameter(obj_loc: &ZeroSpan, } } + // Two assignments in the same template or object clash even when a + // higher-ranked assignment overrides both (DMLC ENAMECOLL) + let mut overridden: Vec<_> = sorted_definitions.iter() + .filter(|def|!overriding_defs.contains(def)).collect(); + overridden.sort_by_key(|(def, _)|*def.obj.span()); + for (index, (first, rank)) in overridden.iter().enumerate() { + let mut conflicts: Vec<_> = overridden[index + 1..].iter() + .filter(|(other, other_rank)|other_rank.id == rank.id + && other.obj != first.obj + && !first.cond.guaranteed_excluded_from(&other.cond)) + .map(|(other, _)|*other.obj.span()) + .collect(); + conflicts.dedup(); + if !conflicts.is_empty() { + report.push(DMLError { + span: *first.obj.span(), + description: format!( + "Conflicting assignments to parameter '{}'", name), + related: conflicts.into_iter().map(|span| + (span, "Conflicting assignment here".to_string())) + .collect(), + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } + let bad_overrides: Vec<&(ObjectDecl, Rank)> = sorted_definitions.iter().filter( |(def, _)|!def.obj.is_default && !overriding_defs.iter().any( |(def2, _)|def2 == def)).collect(); - if !bad_overrides.is_empty() { + if let (Some((overriding, _)), false) = + (overriding_defs.first(), bad_overrides.is_empty()) { report.push(DMLError { - span: *sorted_definitions.first().unwrap().0.obj.span(), + span: *overriding.obj.span(), description: "Definition overrides non-default parameter" .to_string(), related: bad_overrides.iter().map( @@ -1554,6 +1595,7 @@ fn create_object_instance(loc: Option, parent: None, arraydimvars: array_info.to_vec(), components: HashMap::default(), + resolved_method_calls: HashMap::default(), }; let key = container.insert(obj); container.get_mut(key).unwrap().key = key; @@ -1563,13 +1605,15 @@ fn create_object_instance(loc: Option, fn handle_variable(obj: &mut DMLCompositeObject, vardecl: VariableDecl, init: Option, - kind: VariableKind) { - let (_, typed) = eval_type(&vardecl.typed, (), (), + kind: VariableKind, + types: &TypeDatabase) { + let (_, typed) = eval_type(&vardecl.typed, types, false, None, false); let var = DMLVariable { declaration: Declaration { name: vardecl.object.name, type_ref: typed.into(), + type_syntax: vardecl.typed, }, init, }; @@ -1857,6 +1901,7 @@ fn report_dimensionality_conflicts( fn collect_symbols(parameters: &[DMLParameter], obj_specs: &[AppliedObjectSpec], eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> CollectedSymbols { // Iterate over specs to collect object-decls for each separate kind and collect @@ -1968,8 +2013,12 @@ fn collect_symbols(parameters: &[DMLParameter], for error in errors { report.push(DMLError { span: *error.span(), - // TODO: early-evaluate the error message, somehow - description: "unguarded error statement".to_string(), + // Mirrors DMLC's EERRSTMT for object-level 'error' + description: match error.obj.message.as_deref() { + Some(ExpressionKind::StringLiteral(message)) => + message.val.clone(), + _ => "explicit error".to_string(), + }, related: vec![], severity: Some(DiagnosticSeverity::ERROR), }); @@ -2020,7 +2069,7 @@ fn collect_symbols(parameters: &[DMLParameter], for (rank, method) in method_decls { let to_insert = (rank.clone(), method.cond.clone(), - MethodDecl::from_content(&method.obj, report)); + MethodDecl::from_content(&method.obj, types, report)); let name = method.obj.object.name.val.clone(); if let Some((_, e)) = methods.get_mut(&name) { e.push(to_insert); @@ -2233,6 +2282,7 @@ fn merge_composite_subobj<'c>(name: String, parent_key: Option, container: &'c mut StructureContainer, eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> StructureKey { debug!("Merging a composite subobj for {}", name); let (auth_obj, _) = &specs.first().unwrap(); @@ -2248,13 +2298,34 @@ fn merge_composite_subobj<'c>(name: String, DimensionalityCandidate { condition: decl.cond.clone(), span: if decl.obj.dims.is_empty() { - *decl.obj.span() + decl.obj.object.name.span } else { combine_vec_of_decls(&decl.obj.dims) }, dimensions: decl.obj.dims.len(), }).collect(), report); + for (decl, _) in &specs { + let mut index_sites: HashMap<&str, &ZeroSpan> = HashMap::default(); + for dim in &decl.obj.dims { + if dim.indexvar.val == "_" { + continue; + } + if let Some(previous) = index_sites.insert( + &dim.indexvar.val, &dim.indexvar.span) { + report.push(DMLError { + span: dim.indexvar.span, + description: format!( + "Name collision on index variable '{}'", + dim.indexvar.val), + related: vec![(*previous, + "Previously defined here".to_string())], + severity: Some(DiagnosticSeverity::ERROR), + }); + } + } + } + // Vec of (auth, mismatching name) array declarations let mut array_info: Vec<(ArrayDim, Vec<&ZeroSpan>)> = auth_obj.obj.dims.iter() @@ -2269,7 +2340,10 @@ fn merge_composite_subobj<'c>(name: String, } for ((auth_decl, mismatches), other_decl) in array_info.iter_mut().zip(decl.obj.dims.iter()) { - if auth_decl.indexvar.val != other_decl.indexvar.val { + if auth_decl.indexvar.val == "_" { + auth_decl.indexvar = other_decl.indexvar.clone(); + } else if other_decl.indexvar.val != "_" + && auth_decl.indexvar.val != other_decl.indexvar.val { mismatches.push(&other_decl.indexvar.span) } if auth_decl.size.is_none() { @@ -2313,6 +2387,7 @@ fn merge_composite_subobj<'c>(name: String, parent_key, container, eval_context, + types, report) } @@ -2321,6 +2396,7 @@ fn merge_composite_subobjs<'c>(parent_each_stmts: &AppliedInEachSpec, parent_key: Option, container: &'c mut StructureContainer, eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> Vec { debug!("Merging composite subobjects"); @@ -2336,6 +2412,7 @@ fn merge_composite_subobjs<'c>(parent_each_stmts: &AppliedInEachSpec, parent_key, container, eval_context, + types, report)) } else { None @@ -2459,6 +2536,43 @@ fn sort_method_decls<'t>(decls: &[(&Rank, &ExistCondition, (method_map_defs, method_order, method_map_decls) } +// Some definition must override all others that can coexist with it +fn report_ambiguous_method_definitions( + obj: &DMLCompositeObject, + name: &str, + all_decls: &[(&Rank, &ExistCondition, &MethodDecl)], + default_map: &DeclMap<'_>, + report: &mut Vec) { + let overridden: HashSet<&MethodDecl> = default_map.values() + .flatten().copied().collect(); + let condition_of = |method: &MethodDecl| all_decls.iter() + .find(|(_, _, decl)|*decl == method) + .map(|(rank, condition, _)|(*rank, *condition)); + let mut tops: Vec<&MethodDecl> = default_map.keys().copied() + .filter(|method|!overridden.contains(method)).collect(); + tops.sort_by_key(|method|*method.location()); + // Same-rank duplicates are reported as name collisions instead + let coexisting = tops.iter().enumerate().any(|(index, first)| + tops[index + 1..].iter().any(|second| + match (condition_of(first), condition_of(second)) { + (Some((first_rank, first)), Some((second_rank, second))) => + first_rank != second_rank + && !first.guaranteed_excluded_from(second), + _ => true, + })); + if coexisting { + report.push(DMLError { + span: obj.declloc, + description: format!( + "Conflicting definitions of method '{}' in {}", + name, obj.identity()), + related: tops.iter().map(|method|(*method.location(), + "Conflicting definition here".to_string())).collect(), + severity: Some(DiagnosticSeverity::ERROR), + }); + } +} + fn add_methods(obj: &mut DMLCompositeObject, methods: MethodMapping, decl_to_trait_map: &HashMap>, @@ -2477,6 +2591,8 @@ fn add_methods(obj: &mut DMLCompositeObject, (rank, condition, declaration)).collect(); let (default_map, method_order, decl_map) = sort_method_decls(&all_decls); + report_ambiguous_method_definitions( + obj, &name, &all_decls, &default_map, report); let mut decl_to_method: HashMap> = HashMap::default(); // Create abstract methodrefs for declarations first, order does not matter @@ -2532,11 +2648,12 @@ fn add_methods(obj: &mut DMLCompositeObject, if !decl.default && !decl.is_abstract() { report.push(DMLError { - span: decl.name.span, + span: method.name.span, description: "Cannot override non-default method".into(), - related: vec![(method.name.span, - "overridden here".to_string())], + related: vec![(decl.name.span, + "Non-default method declared here" + .to_string())], severity: Some(DiagnosticSeverity::ERROR), }); } @@ -2678,7 +2795,9 @@ fn check_trait_overrides(obj: &DMLCompositeObject, *member.location(), report); } - if let Some(meth) = member.as_method() { + // Non-shared overrides are checked when adding methods + if let Some(meth) = member.as_method() + .filter(|meth|meth.is_shared()) { if let DMLResolvedObject::ShallowObject( DMLShallowObject { variant: DMLShallowObjectVariant::Method(m), @@ -2770,10 +2889,13 @@ fn make_auto_params(identity: &DMLString, let mut indices = vec![]; for arraydim in index_info { - let new_decl = make_objectdecl_auto_param( - &arraydim.indexvar.val, &arraydim.indexvar.span, - arraydim.size.as_ref().map(|e|ParamValue::Set(e.clone()))); - auto_parameters.insert(arraydim.indexvar.val.to_string(), new_decl); + if arraydim.indexvar.val != "_" { + let new_decl = make_objectdecl_auto_param( + &arraydim.indexvar.val, &arraydim.indexvar.span, + arraydim.size.as_ref().map(|e|ParamValue::Set(e.clone()))); + auto_parameters.insert(arraydim.indexvar.val.to_string(), + new_decl); + } // Note: the location of this identifier is fake and will point to // the index declaration (this is probably fine) indices.push(Box::new(ExpressionKind::Identifier(Identifier { @@ -2856,6 +2978,7 @@ fn make_object(loc: ZeroSpan, parent_key: Option, container: &mut StructureContainer, eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> StructureKey { debug!("Making object {}", identity.val); @@ -2891,7 +3014,7 @@ fn make_object(loc: ZeroSpan, hooks, subobjs, externs, - } = collect_symbols(¶meters, &obj_specs, eval_context, report); + } = collect_symbols(¶meters, &obj_specs, eval_context, types, report); trace!("All local symbols are: {:?}", symbols.keys().map(|k|k.as_str()).collect::>()); @@ -2903,7 +3026,7 @@ fn make_object(loc: ZeroSpan, let subobj_keys = merge_composite_subobjs(&each_stmts, subobjs, Some(new_obj_key), container, - eval_context, report); + eval_context, types, report); { let new_obj = container.get_mut(new_obj_key).unwrap(); @@ -2913,8 +3036,8 @@ fn make_object(loc: ZeroSpan, add_constants(new_obj, constants); add_parameters(new_obj, parameters); add_hooks(new_obj, hooks); - add_saveds(new_obj, saveds); - add_sessions(new_obj, sessions); + add_saveds(new_obj, saveds, types); + add_sessions(new_obj, sessions, types); } // We need to drop the new_obj reference here, so that we can // access multiple keys simultaneously from the storage @@ -2936,6 +3059,55 @@ fn make_object(loc: ZeroSpan, new_obj_key } +pub fn check_method_body_types_for_objects( + container: &StructureContainer, + types: &TypeDatabase, + report: &mut Vec, +) { + // 'live' is false once the chain passes a method that never calls default + fn collect_method_chain(method: &Arc, + live: bool, + seen: &mut HashMap, + methods: &mut Vec<(Arc, bool)>) { + if let Some(index) = seen.get(method.location()) { + if live && !methods[*index].1 { + methods[*index].1 = true; + } else { + return; + } + } else { + seen.insert(*method.location(), methods.len()); + methods.push((Arc::clone(method), live)); + } + if let Some(default) = method.get_default() { + let live = live && method.get_decl().calls_default; + for method in default.flat_refs() { + collect_method_chain(method, live, seen, methods); + } + } + } + + for object in container.values() { + let effective_methods: Vec<_> = object.components.values() + .filter_map(|component|component.as_shallow() + .and_then(|shallow|match &shallow.variant { + DMLShallowObjectVariant::Method(method) => + Some(Arc::clone(method)), + _ => None, + })) + .collect(); + let mut methods_to_check = vec![]; + let mut seen_methods = HashMap::default(); + for method in &effective_methods { + collect_method_chain( + method, true, &mut seen_methods, &mut methods_to_check); + } + for (method, live) in methods_to_check { + check_method_body_types(&method, object, types, live, report); + } + } +} + #[cfg(test)] mod tests { use std::sync::{Arc, OnceLock}; diff --git a/src/analysis/templating/topology.rs b/src/analysis/templating/topology.rs index af026861..b53f4544 100644 --- a/src/analysis/templating/topology.rs +++ b/src/analysis/templating/topology.rs @@ -25,6 +25,7 @@ use crate::analysis::templating::objects::{create_objectspec}; use crate::analysis::templating::traits::{TemplateTraitInfo, DMLTemplate, DMLTrait}; +use crate::analysis::templating::types::TypeDatabase; use crate::file_management::CanonPath; lazy_static! { @@ -173,6 +174,7 @@ pub fn create_templates_traits<'t>( imp_map: &'t HashMap, rank_struct: HashMap<&'t str, InEachStruct<'t>>, eval_context: &mut EvaluationContext, + types: &TypeDatabase, report: &mut Vec) -> TemplateTraitInfo { let mut templates = HashMap::default(); @@ -201,7 +203,7 @@ pub fn create_templates_traits<'t>( // also creating traits in any case, just with nothing // in them let traitspec = DMLTrait::process(templname, templ.get_def_loc(), - &spec, &traits, report); + &spec, &traits, types, report); traits.insert(templname.to_string(), Arc::clone(&traitspec)); let template = DMLTemplate::make( templname, @@ -593,9 +595,11 @@ pub fn rank_templates_aux<'t>(mut templates: HashMap<&'t str, unconditional_references, } = dependencies(template.get_spec(), imp_map, eval_context, report); let referenced: HashSet<&'t str> - = inferior.keys().filter(|s|!invalid_isimps.values().flatten(). - any(|s2|&s2 == s)) - .cloned().collect(); + = inferior.iter().filter( + |(name, kind)| invalid_isimps.get(kind) + .is_none_or(|invalid_names| !invalid_names.contains(name))) + .map(|(name, _)| *name) + .collect(); trace!("Template {:?} requires {:?}", template.get_name(), referenced); required_templates.insert(template.get_name(), referenced.clone()); let all_missing: HashSet<&'t str> = referenced.difference( diff --git a/src/analysis/templating/traits.rs b/src/analysis/templating/traits.rs index 11e3774f..5b5a928d 100644 --- a/src/analysis/templating/traits.rs +++ b/src/analysis/templating/traits.rs @@ -7,7 +7,7 @@ use std::iter; use std::sync::Arc; use crate::analysis::parsing::tree::ZeroSpan; -use crate::analysis::structure::objects::{MaybeAbstract}; +use crate::analysis::structure::objects::{MaybeAbstract, MethodModifier}; use crate::analysis::structure::expressions::DMLString; use crate::analysis::structure::toplevel::{ObjectDecl, ExistCondition}; @@ -18,7 +18,7 @@ use crate::analysis::templating::objects::{CoarseObjectKind, DMLCoarseObjectKind, ObjectSpec, Spec}; use crate::analysis::templating::Declaration; -use crate::analysis::templating::types::eval_type_simple; +use crate::analysis::templating::types::{eval_type_simple, TypeDatabase}; use crate::analysis::templating::topology::Rank; use crate::analysis::DeclarationSpan; @@ -147,13 +147,14 @@ where T: Borrow { pub fn get_impls(me: &Arc) -> HashMap> { trace!("Getting all impls of {:?}", me.name); - me.sessions.keys() - .chain(me.saveds.keys()) - .chain(me.params.keys()) - .chain(me.methods.keys()) - .chain(me.abstract_methods.keys()) - .map(|name|(name.to_string(), Arc::clone(me))) - .chain(me.ancestor_map.iter().map(|(n, rc)|(n.clone(), Arc::clone(rc)))) + // Own members come last so they replace inherited ones they override + me.ancestor_map.iter().map(|(n, rc)|(n.clone(), Arc::clone(rc))) + .chain(me.sessions.keys() + .chain(me.saveds.keys()) + .chain(me.params.keys()) + .chain(me.methods.keys()) + .chain(me.abstract_methods.keys()) + .map(|name|(name.to_string(), Arc::clone(me)))) .collect() } @@ -258,6 +259,7 @@ impl DMLTrait { maybeloc: Option<&ZeroSpan>, spec: &Arc, traits: &HashMap>, + types: &TypeDatabase, report: &mut Vec) -> Arc { debug!("Processing trait for {}", name); @@ -309,7 +311,13 @@ impl DMLTrait { s})); // Check that unification of ancestors implementations are sound - let impl_map = merge_impl_maps(name, loc, parents.values(), report); + let own_methods: HashSet = spec.methods.iter() + .filter_map(discard_conditional) + .filter(|method|!method.is_abstract()) + .map(|method|crate::analysis::DMLNamed::name(&method).val.clone()) + .collect(); + let impl_map = merge_impl_maps(name, loc, parents.values(), + &own_methods, report); // Check that our implementations do not collide let mut used_names = HashMap::default(); @@ -350,11 +358,9 @@ impl DMLTrait { .flat_map(|sess|sess.vars) .map(|decl|Declaration { name: decl.object.name.clone(), + type_syntax: decl.typed.clone(), type_ref: { - // TODO: How to actually evaluate this? - // Needs global scope - let (_, typ) = eval_type_simple( - &decl.typed, (), ()); + let (_, typ) = eval_type_simple(&decl.typed, types); typ.into() } }).filter(|d|maybe_report_collision( @@ -369,11 +375,9 @@ impl DMLTrait { .flat_map(|sav|sav.vars) .map(|decl|Declaration { name: decl.object.name.clone(), + type_syntax: decl.typed.clone(), type_ref: { - // TODO: How to actually evaluate this? - // Needs global scope - let (_, typ) = eval_type_simple( - &decl.typed, (), ()); + let (_, typ) = eval_type_simple(&decl.typed, types); typ.into() } }).filter(|d|maybe_report_collision( @@ -389,11 +393,9 @@ impl DMLTrait { .filter(|param|param.typed.is_some()) .map(|param|Declaration { name: param.object.name.clone(), + type_syntax: param.typed.clone().unwrap(), type_ref: { - // TODO: How to actually evaluate this? - // Needs global scope - let (_, typ) = eval_type_simple( - ¶m.typed.unwrap(), (), ()); + let (_, typ) = eval_type_simple(¶m.typed.unwrap(), types); typ.into() } }).filter(|d|maybe_report_collision( @@ -408,7 +410,12 @@ impl DMLTrait { .filter_map(discard_conditional) .filter(|method|method.is_abstract()) .filter_map(|m| { - let new_m = MethodDecl::from_content(&m, report); + // Non-shared signatures are checked in instantiating objects + let mut discarded = vec![]; + let new_m = MethodDecl::from_content( + &m, types, + if m.modifier == MethodModifier::Shared { &mut *report } + else { &mut discarded }); if maybe_report_collision( &mut used_names, new_m.identity().to_string(), @@ -424,7 +431,11 @@ impl DMLTrait { for method in spec.methods.iter() .filter_map(discard_conditional) .filter(|method|!method.is_abstract()) { - let new_m = MethodDecl::from_content(&method, report); + let mut discarded = vec![]; + let new_m = MethodDecl::from_content( + &method, types, + if method.modifier == MethodModifier::Shared { &mut *report } + else { &mut discarded }); // special case, methods are allowed to conflict // with abstract methods // NOTE: Right now this means we'd report the collision @@ -473,7 +484,9 @@ impl DMLTrait { meth.name.val.as_str()) { match coll { TraitMemberKind::Method(undermeth) => { - if meth.is_abstract() { + // Non-shared overrides are checked per object + if !meth.is_shared() { + } else if meth.is_abstract() && undermeth.is_shared() { report.push(DMLError { span: meth.name.span, description: @@ -484,8 +497,7 @@ impl DMLTrait { (*undermeth.location(), "Overridden definition here".into())], }); - } - if !undermeth.is_default() && !undermeth.is_abstract() { + } else if !undermeth.is_default() && !undermeth.is_abstract() { report.push(DMLError { span: meth.name.span, description: @@ -498,7 +510,9 @@ impl DMLTrait { severity: Some(DiagnosticSeverity::ERROR), }); } - meth.check_override(undermeth, report); + if meth.is_shared() { + meth.check_override(undermeth, report); + } ancestor.get_defined_method(meth.identity()) .map(|dmeth|DefaultCallReference::Valid( Arc::new(DMLMethodRef { @@ -539,7 +553,7 @@ impl DMLTrait { DMLConcreteMethod { // TODO: This clone is slightly inefficient decl: meth.clone(), - default_call, + default_call, } ); } @@ -596,6 +610,7 @@ impl DMLTrait { pub fn merge_impl_maps<'t, I>(name: &str, loc: &ZeroSpan, parents_iter: I, + own_methods: &HashSet, report: &mut Vec) -> HashMap> where @@ -650,13 +665,20 @@ where } else if !m1.is_abstract() && m2.is_abstract() { map.insert(name, implr); + } else if !m1.is_abstract() + && own_methods.contains(&name) { + // Our own definition overrides both + map.insert(name, implr); } else { // Both abstract or both non-abstract // ambiguous - map.insert(name.clone(), Arc::clone(&implr)); - ambiguous_defs.entry(name) - .or_default() - .push(mimplr); + let ambiguous = ambiguous_defs + .entry(name.clone()).or_default(); + if ambiguous.is_empty() { + ambiguous.push(mimplr); + } + ambiguous.push(Arc::clone(&implr)); + map.insert(name, implr); } }, _ => unreachable!("should be covered by if clause"), @@ -679,6 +701,14 @@ where } trace!("Ambiguous defs {:?}", ambiguous_defs.keys()); for (ambiguous_name, traits) in &ambiguous_defs { + let mut related: Vec<_> = traits.iter().map( + |t|{ + let decl = t.get_method(ambiguous_name).unwrap(); + (*decl.location(), + format!("Conflicting definition here in {}", + t.name)) + }).collect(); + related.sort(); // TODO: in some cases we can here report an obvious suggestion // example: templ A contains a default decl and templ B contains // a non-default one, we can then suggest B to implement A @@ -690,18 +720,19 @@ where "Conflicting definitions of method '{}' in {}. \ This might be due to uncertain inheritance order.", ambiguous_name, name), - related: traits.iter().map( - |t|{ - let decl = t.get_method(ambiguous_name).unwrap(); - (*decl.location(), - format!("Conflicting definition here in {}", - t.name)) - }).collect(), + related, severity: Some(DiagnosticSeverity::ERROR), }); } trace!("Conflicting defs {:?}", conflicting_defs); for (conflict_name, traits) in &conflicting_defs { + let mut related: Vec<_> = traits.iter().map(|t|{ + let decl = t.get_member(conflict_name).unwrap(); + (*decl.location(), + format!("Conflicting definition here in {}", + t.name)) + }).collect(); + related.sort(); // TODO: in some cases we can here report an obvious suggestion // example: templ A contains a default decl and templ B contains // a non-default one, we can then suggest B to implement A @@ -710,12 +741,7 @@ where description: format!( "Conflicting definitions of '{}' in {}.", conflict_name, name), - related: traits.iter().map(|t|{ - let decl = t.get_member(conflict_name).unwrap(); - (*decl.location(), - format!("Conflicting definition here in {}", - t.name)) - }).collect(), + related, severity: Some(DiagnosticSeverity::ERROR), }); } diff --git a/src/analysis/templating/types.rs b/src/analysis/templating/types.rs index bead0f92..58962b59 100644 --- a/src/analysis/templating/types.rs +++ b/src/analysis/templating/types.rs @@ -1,45 +1,798 @@ // © 2024 Intel Corporation // SPDX-License-Identifier: Apache-2.0 and MIT +use std::collections::HashMap; +use std::convert::TryFrom; + +use lsp_types::DiagnosticSeverity; + +use crate::analysis::DMLError; use crate::analysis::parsing::tree::ZeroSpan; +use crate::analysis::structure::expressions::{DMLString, ExpressionKind}; +use crate::analysis::structure::objects::Typedef; +use crate::analysis::structure::types::{DMLType, DMLTypeKind, DMLTypeMember}; +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct TypeId(usize); -use crate::analysis::structure::types::DMLType; +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum TypeUse { + Declared(TypeId), + // Struct, layout or bitfields type without a name, identified by its span + Anonymous(ZeroSpan), +} -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct DMLBaseType { - no_info: ZeroSpan, +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TypeDeclaration { + pub id: TypeId, + pub name: DMLString, + pub declaration_span: ZeroSpan, + pub underlying: Option, + pub is_extern: bool, } -impl DMLBaseType { - pub fn span(&self) -> &ZeroSpan { - &self.no_info +#[derive(Debug, Clone, Default)] +pub struct TypeDatabase { + declarations: Vec, + typedefs_by_name: HashMap>, +} + +impl TypeDatabase { + pub fn report_unknown_types(&self, + typed: &DMLType, + report: &mut Vec) { + match &typed.kind { + DMLTypeKind::Named(name) => + if !is_builtin_type(&name.val) + && self.typedefs_named(&name.val).is_empty() { + report.push(DMLError { + span: name.span, + description: format!("Unknown type: '{}'", name.val), + related: vec![], + severity: Some(DiagnosticSeverity::ERROR), + }); + }, + DMLTypeKind::Struct(members) | + DMLTypeKind::Layout { fields: members, .. } => + for member in members { + self.report_unknown_types(&member.typed, report); + }, + DMLTypeKind::Bitfields { fields, .. } => + for field in fields { + self.report_unknown_types(&field.member.typed, report); + }, + DMLTypeKind::Hook(arguments) => + for argument in arguments { + self.report_unknown_types(argument, report); + }, + DMLTypeKind::Const(inner) | + DMLTypeKind::Pointer(inner) | + DMLTypeKind::Vector(inner) => + self.report_unknown_types(inner, report), + DMLTypeKind::Array { element, .. } => + self.report_unknown_types(element, report), + DMLTypeKind::Function { returns, parameters, .. } => { + self.report_unknown_types(returns, report); + for parameter in parameters { + self.report_unknown_types(parameter, report); + } + }, + DMLTypeKind::TypeOf(_) | DMLTypeKind::Sequence(_) | + DMLTypeKind::Unknown => (), + } + } + + pub fn add_typedef(&mut self, typedef: &Typedef) -> TypeId { + let id = TypeId(self.declarations.len()); + self.declarations.push(TypeDeclaration { + id, + name: typedef.object.name.clone(), + declaration_span: typedef.object.name.span, + underlying: Some(typedef.typed.clone()), + is_extern: typedef.is_extern, + }); + self.typedefs_by_name.entry(typedef.object.name.val.clone()) + .or_default().push(id); + id + } + + pub fn add_template(&mut self, name: DMLString) -> TypeId { + let id = TypeId(self.declarations.len()); + self.declarations.push(TypeDeclaration { + id, + declaration_span: name.span, + name: name.clone(), + underlying: None, + is_extern: false, + }); + self.typedefs_by_name.entry(name.val).or_default().push(id); + id + } + + pub fn typedefs_named(&self, name: &str) -> &[TypeId] { + self.typedefs_by_name.get(name).map(Vec::as_slice).unwrap_or(&[]) + } + + pub fn declaration(&self, id: TypeId) -> Option<&TypeDeclaration> { + self.declarations.get(id.0) + } + + pub fn declarations(&self) -> impl Iterator { + self.declarations.iter() + } + + pub fn resolve_type_use(&self, typed: &DMLType) -> Option { + self.resolve_type_shape(typed).map(|(type_use, _)|type_use) + } + + // The type use under 'typed', and how many pointer/array layers wrap it + pub fn resolve_type_shape(&self, typed: &DMLType) + -> Option<(TypeUse, u32)> { + match &typed.kind { + DMLTypeKind::Named(name) => match self.typedefs_named(&name.val) { + [type_id] => Some((TypeUse::Declared(*type_id), 0)), + _ => None, + }, + DMLTypeKind::Struct(_) | + DMLTypeKind::Layout { .. } | + DMLTypeKind::Bitfields { .. } => + Some((TypeUse::Anonymous(typed.span), 0)), + DMLTypeKind::Const(inner) => self.resolve_type_shape(inner), + DMLTypeKind::Pointer(inner) | + DMLTypeKind::Vector(inner) | + DMLTypeKind::Array { element: inner, .. } => + self.resolve_type_shape(inner) + .map(|(type_use, layers)|(type_use, layers + 1)), + DMLTypeKind::Function { returns, .. } => + self.resolve_type_shape(returns), + _ => None, + } + } + + // Whether the type under 'typed', as found by 'resolve_type_shape', is a + // 'typeof' that we cannot resolve + pub fn is_typeof_shape(&self, typed: &DMLType) -> bool { + match &typed.kind { + DMLTypeKind::TypeOf(_) => true, + DMLTypeKind::Const(inner) | + DMLTypeKind::Pointer(inner) | + DMLTypeKind::Vector(inner) | + DMLTypeKind::Array { element: inner, .. } | + DMLTypeKind::Function { returns: inner, .. } => + self.is_typeof_shape(inner), + _ => false, + } + } + + // Follows typedefs from 'type_use' to a struct-like type, counting the + // pointer/array layers the typedefs add on the way + pub fn struct_core(&self, type_use: TypeUse) -> Option<(TypeUse, u32)> { + let mut current = type_use; + let mut layers = 0; + let mut visited = vec![]; + loop { + let TypeUse::Declared(type_id) = current else { + return Some((current, layers)); + }; + if visited.contains(&type_id) { + return None; + } + visited.push(type_id); + let underlying = self.declaration(type_id)?.underlying.as_ref()?; + if direct_members(underlying).is_some() { + return Some((current, layers)); + } + let (next, extra) = self.resolve_type_shape(underlying)?; + current = next; + layers += extra; + } + } +} + +pub fn direct_members(typed: &DMLType) -> Option> { + match &typed.kind { + DMLTypeKind::Struct(members) | + DMLTypeKind::Layout { fields: members, .. } => + Some(members.iter().collect()), + DMLTypeKind::Bitfields { fields, .. } => + Some(fields.iter().map(|field|&field.member).collect()), + _ => None, } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DMLStructType { - no_info: ZeroSpan, + span: ZeroSpan, } impl DMLStructType { pub fn span(&self) -> &ZeroSpan { - &self.no_info + &self.span + } +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DMLResolvedMember { + pub name: Option, + pub typed: DMLConcreteType, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum DMLConcreteTypeKind { + Named(String), + Template(TypeId), + Typedef { + id: TypeId, + underlying: Option>, + }, + Struct { + identity: ZeroSpan, + fields: Vec, + }, + Layout { + identity: ZeroSpan, + byte_order: Option, + fields: Vec, + }, + Bitfields { + identity: ZeroSpan, + width: Option, + fields: Vec, + }, + TypeOf, + Sequence { + template: Option, + name: String, + }, + Hook(Vec), + Const(Box), + Pointer(Box), + Vector(Box), + Array { + element: Box, + size: DMLArraySize, + }, + Function { + returns: Box, + parameters: Vec, + variadic: bool, + }, + Unknown, +} + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum DMLArraySize { + Unspecified, + Constant(u64), + Expression(Box), +} + +impl DMLArraySize { + fn constant(&self) -> Option { + match self { + Self::Constant(value) => Some(*value), + Self::Unspecified | Self::Expression(_) => None, + } + } + + fn description(&self) -> String { + match self { + Self::Unspecified => String::new(), + Self::Constant(value) => value.to_string(), + Self::Expression(_) => "...".to_string(), + } + } + + fn equivalent(&self, other: &Self) -> bool { + self == other } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum DMLConcreteType { - BaseType(DMLBaseType), - StructType(DMLStructType), +pub struct DMLConcreteType { + pub kind: DMLConcreteTypeKind, + pub span: ZeroSpan, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum IntegerTruncation { + AllTargets, + Windows, + NonWindows, +} + +impl IntegerTruncation { + pub fn target_suffix(self) -> &'static str { + match self { + IntegerTruncation::AllTargets => "", + IntegerTruncation::Windows => " on Windows targets", + IntegerTruncation::NonWindows => " on non-Windows targets", + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StorageCompatibility { + Compatible, + Truncates(IntegerTruncation), + Incompatible, +} + +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum TypeComparisonMode { + Strict, + #[default] + Fuzzy, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum StorageValue { + Integer(i128), +} + +pub fn check_storage_compatibility( + target: &DMLConcreteType, + source: &DMLConcreteType, + value: Option, +) -> StorageCompatibility { + let compatibility = target.storage_compatibility(source); + if compatibility == StorageCompatibility::Incompatible { + return compatibility; + } + match value { + Some(StorageValue::Integer(value)) => target + .integer_value_truncation(value) + .map(StorageCompatibility::Truncates) + .unwrap_or(StorageCompatibility::Compatible), + None => compatibility, + } +} + +impl StorageCompatibility { + pub fn is_compatible(self) -> bool { + !matches!(self, StorageCompatibility::Incompatible) + } } impl DMLConcreteType { pub fn span(&self) -> &ZeroSpan { - match self { - DMLConcreteType::BaseType(b) => b.span(), - DMLConcreteType::StructType(s) => s.span(), + &self.span + } + + pub fn description(&self, types: &TypeDatabase) -> String { + use DMLConcreteTypeKind::*; + + match &self.kind { + Named(name) => name.clone(), + Template(id) | Typedef { id, .. } => types.declaration(*id) + .map(|declaration|declaration.name.val.clone()) + .unwrap_or_else(||"unknown".to_string()), + Struct { .. } => "struct".to_string(), + Layout { .. } => "layout".to_string(), + Bitfields { .. } => "bitfields".to_string(), + TypeOf => "typeof(...)".to_string(), + Sequence { name, .. } => format!("sequence({})", name), + Hook(_) => "hook".to_string(), + Const(inner) => format!("const {}", inner.description(types)), + Pointer(inner) => format!("{} *", inner.description(types)), + Vector(inner) => format!("vector<{}>", inner.description(types)), + Array { element, size } => format!( + "{}[{}]", element.description(types), + size.description()), + Function { .. } => "function".to_string(), + Unknown => "unknown".to_string(), + } + } + + pub fn array_element(&self) -> Option<&DMLConcreteType> { + match &self.kind { + DMLConcreteTypeKind::Array { element, .. } => Some(element), + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } | DMLConcreteTypeKind::Const(underlying) => + underlying.array_element(), + _ => None, + } + } + + pub fn array_length(&self) -> Option { + match &self.kind { + DMLConcreteTypeKind::Array { size, .. } => size.constant(), + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } | DMLConcreteTypeKind::Const(underlying) => + underlying.array_length(), + _ => None, + } + } + + pub fn can_store(&self, source: &DMLConcreteType) -> bool { + self.storage_compatibility(source).is_compatible() + } + + pub fn storage_compatibility( + &self, + source: &DMLConcreteType, + ) -> StorageCompatibility { + self.storage_compatibility_with_mode(source, TypeComparisonMode::default()) + } + + pub fn storage_compatibility_with_mode( + &self, + source: &DMLConcreteType, + mode: TypeComparisonMode, + ) -> StorageCompatibility { + use DMLConcreteTypeKind::*; + + if matches!(self.kind, Unknown | TypeOf) + || matches!(source.kind, Unknown | TypeOf) { + return StorageCompatibility::Compatible; + } + if let (Some(target), Some(source)) = + (integer_storage_type(self), integer_storage_type(source)) { + return target.storage_compatibility(source); + } + if matches!(self.kind, Bitfields { .. }) { + return StorageCompatibility::Incompatible; + } + + match (&self.kind, &source.kind) { + (Typedef { underlying: Some(underlying), .. }, _) => + underlying.storage_compatibility_with_mode(source, mode), + (_, Typedef { underlying: Some(underlying), .. }) => + self.storage_compatibility_with_mode(underlying, mode), + (Const(target), _) => + target.storage_compatibility_with_mode(source, mode), + (_, Const(source)) => + self.storage_compatibility_with_mode(source, mode), + (Named(target), Named(source)) => + named_type_storage_compatibility(target, source), + (Pointer(target), Pointer(source)) | + (Pointer(target), Array { element: source, .. }) => + if pointer_target_can_store(target, source, mode) { + StorageCompatibility::Compatible + } else { + StorageCompatibility::Incompatible + }, + _ if self.equivalent(source) => StorageCompatibility::Compatible, + _ => StorageCompatibility::Incompatible, + } + } + + pub fn integer_value_truncation(&self, value: i128) + -> Option { + use DMLConcreteTypeKind::{Bitfields, Const, Named, Typedef}; + + match &self.kind { + Typedef { underlying: Some(underlying), .. } | + Const(underlying) => underlying.integer_value_truncation(value), + Named(name) => integer_type(name) + .and_then(|target|target.value_truncation(value)), + Bitfields { width, .. } => width.as_deref() + .and_then(bitfield_integer_type) + .and_then(|target|target.value_truncation(value)), + _ => None, + } + } + + fn equivalent(&self, other: &DMLConcreteType) -> bool { + use DMLConcreteTypeKind::*; + + match (&self.kind, &other.kind) { + (Unknown | TypeOf, _) | (_, Unknown | TypeOf) => true, + (Typedef { id: left, .. }, Typedef { id: right, .. }) + if left == right => true, + (Typedef { underlying: Some(underlying), .. }, _) => + underlying.equivalent(other), + (_, Typedef { underlying: Some(underlying), .. }) => + self.equivalent(underlying), + (Named(left), Named(right)) => left == right, + (Template(left), Template(right)) => left == right, + (Typedef { id: left, underlying: None }, + Typedef { id: right, underlying: None }) => left == right, + (Struct { identity: left_identity, .. }, + Struct { identity: right_identity, .. }) => + left_identity == right_identity, + (Layout { + identity: left_identity, + .. + }, Layout { + identity: right_identity, + .. + }) => left_identity == right_identity, + (Bitfields { + width: left_width, + fields: left, + .. + }, Bitfields { + width: right_width, + fields: right, + .. + }) => left_width == right_width + && members_equivalent(left, right), + (Sequence { template: left, name: left_name }, + Sequence { template: right, name: right_name }) => + left == right && left_name == right_name, + (Hook(left), Hook(right)) => types_equivalent(left, right), + (Const(left), Const(right)) | + (Pointer(left), Pointer(right)) | + (Vector(left), Vector(right)) => left.equivalent(right), + (Array { element: left, size: left_size }, + Array { element: right, size: right_size }) => + left_size.equivalent(right_size) && left.equivalent(right), + (Function { + returns: left_returns, + parameters: left_parameters, + variadic: left_variadic, + }, Function { + returns: right_returns, + parameters: right_parameters, + variadic: right_variadic, + }) => left_variadic == right_variadic + && left_returns.equivalent(right_returns) + && types_equivalent(left_parameters, right_parameters), + _ => false, + } + } + + fn equivalent_fuzzy(&self, other: &DMLConcreteType) -> bool { + fn unqualified(typed: &DMLConcreteType) -> &DMLConcreteType { + match &typed.kind { + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } | DMLConcreteTypeKind::Const(underlying) => + unqualified(underlying), + _ => typed, + } + } + + let left = unqualified(self); + let right = unqualified(other); + use DMLConcreteTypeKind::*; + match (&left.kind, &right.kind) { + (Unknown | TypeOf, _) | (_, Unknown | TypeOf) => true, + (Pointer(left), Pointer(right)) | + (Pointer(left), Array { element: right, .. }) | + (Array { element: left, .. }, Pointer(right)) | + (Array { element: left, .. }, Array { element: right, .. }) => + matches!(&unqualified(left).kind, Named(name) if name == "void") + || matches!(&unqualified(right).kind, + Named(name) if name == "void") + || left.equivalent_fuzzy(right), + (Vector(left), Vector(right)) => left.equivalent_fuzzy(right), + (Hook(left), Hook(right)) => left.len() == right.len() + && left.iter().zip(right).all(|(left, right)| + left.equivalent_fuzzy(right)), + (Function { + returns: left_returns, + parameters: left_parameters, + variadic: left_variadic, + }, Function { + returns: right_returns, + parameters: right_parameters, + variadic: right_variadic, + }) => left_variadic == right_variadic + && left_returns.equivalent_fuzzy(right_returns) + && left_parameters.len() == right_parameters.len() + && left_parameters.iter().zip(right_parameters) + .all(|(left, right)|left.equivalent_fuzzy(right)), + _ => left.equivalent(right), + } + } +} + +fn named_type_storage_compatibility(target: &str, source: &str) + -> StorageCompatibility { + if target == "bool" { + return if source == "bool" || source == "uint1" { + StorageCompatibility::Compatible + } else { + StorageCompatibility::Incompatible + }; + } + if source == "bool" { + return if matches!(target, "int1" | "uint1") { + StorageCompatibility::Compatible + } else { + StorageCompatibility::Incompatible + }; + } + if let Some(target) = integer_type(target) { + if let Some(source) = integer_type(source) { + return target.storage_compatibility(source); + } + return if is_float_type(source) { + StorageCompatibility::Truncates(IntegerTruncation::AllTargets) + } else { + StorageCompatibility::Incompatible + }; + } + if is_float_type(target) { + return if is_integer_type(source) || is_float_type(source) { + StorageCompatibility::Compatible + } else { + StorageCompatibility::Incompatible + }; + } + if target == source { + StorageCompatibility::Compatible + } else { + StorageCompatibility::Incompatible + } +} + +fn is_integer_type(name: &str) -> bool { + integer_type(name).is_some() +} + +fn bitfield_integer_type(width: &str) -> Option { + width.parse::().ok() + .filter(|width|*width > 0) + .map(|width|IntegerType::fixed(width, false)) +} + +fn integer_storage_type(typed: &DMLConcreteType) -> Option { + match &typed.kind { + DMLConcreteTypeKind::Named(name) => integer_type(name), + DMLConcreteTypeKind::Bitfields { width, .. } => + width.as_deref().and_then(bitfield_integer_type), + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } | DMLConcreteTypeKind::Const(underlying) => + integer_storage_type(underlying), + _ => None, + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct IntegerWidths { + windows: u16, + non_windows: u16, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct IntegerType { + widths: IntegerWidths, + signed: bool, +} + +impl IntegerType { + fn fixed(width: u16, signed: bool) -> Self { + Self { + widths: IntegerWidths { + windows: width, + non_windows: width, + }, + signed, + } + } + + fn storage_compatibility(self, source: Self) -> StorageCompatibility { + // Match DMLC's IntegerType.canstore: signedness does not affect + // assignment compatibility, while a wider source may be truncated. + truncation( + source.widths.windows > self.widths.windows, + source.widths.non_windows > self.widths.non_windows, + ).map(StorageCompatibility::Truncates) + .unwrap_or(StorageCompatibility::Compatible) + } + + fn value_truncation(self, value: i128) -> Option { + truncation( + !integer_value_fits(value, self.widths.windows, self.signed), + !integer_value_fits(value, self.widths.non_windows, self.signed), + ) + } +} + +fn truncation(windows: bool, non_windows: bool) -> Option { + match (windows, non_windows) { + (true, true) => Some(IntegerTruncation::AllTargets), + (true, false) => Some(IntegerTruncation::Windows), + (false, true) => Some(IntegerTruncation::NonWindows), + (false, false) => None, + } +} + +fn integer_value_fits(value: i128, width: u16, signed: bool) -> bool { + if signed { + let limit = 1_i128 << (width - 1); + (-limit..limit).contains(&value) + } else { + value >= 0 && value < (1_i128 << width) + } +} + +fn integer_type(name: &str) -> Option { + match name { + "char" => return Some(IntegerType::fixed(8, true)), + "int" => return Some(IntegerType::fixed(32, true)), + "long" => return Some(IntegerType { + widths: IntegerWidths { windows: 32, non_windows: 64 }, + signed: true, + }), + "ulong" => return Some(IntegerType { + widths: IntegerWidths { windows: 32, non_windows: 64 }, + signed: false, + }), + "ssize_t" | "int64_t" => return Some(IntegerType::fixed(64, true)), + "size_t" | "uint64_t" => return Some(IntegerType::fixed(64, false)), + _ => (), + } + + let (signed, width) = if let Some(width) = name.strip_prefix("uint") { + (false, width) + } else if let Some(width) = name.strip_prefix("int") { + (true, width) + } else { + return None; + }; + let width = width.strip_suffix("_be_t") + .or_else(||width.strip_suffix("_le_t")) + .unwrap_or(width) + .parse::().ok()?; + (1..=64).contains(&width).then(||IntegerType::fixed(width, signed)) +} + +fn is_float_type(name: &str) -> bool { + matches!(name, "float" | "double") +} + +// Mirrors DMLC's parse_type and initial typedefs +fn is_builtin_type(name: &str) -> bool { + integer_type(name).is_some() || is_float_type(name) + || matches!(name, "bool" | "void" | "integer_t" | "uinteger_t") +} + +fn pointer_target_can_store(target: &DMLConcreteType, + source: &DMLConcreteType, + mode: TypeComparisonMode) -> bool { + use DMLConcreteTypeKind::{Const, Named, Typedef}; + + if target.equivalent(source) { + return true; + } + + fn transparent(typed: &DMLConcreteType) -> &DMLConcreteType { + match &typed.kind { + Typedef { underlying: Some(underlying), .. } => transparent(underlying), + _ => typed, } } + + let target = transparent(target); + let source = transparent(source); + let target_const = matches!(target.kind, Const(_)); + let source_const = matches!(source.kind, Const(_)); + let target_unqualified = match &target.kind { + Const(inner) => inner.as_ref(), + _ => target, + }; + let source_unqualified = match &source.kind { + Const(inner) => inner.as_ref(), + _ => source, + }; + if matches!(&target_unqualified.kind, Named(name) if name == "void") + || matches!(&source_unqualified.kind, Named(name) if name == "void") { + return mode == TypeComparisonMode::Fuzzy + || !source_const + || target_const; + } + if source_const && !target_const { + return false; + } + target_unqualified.equivalent(source_unqualified) +} + +fn types_equivalent(left: &[DMLConcreteType], + right: &[DMLConcreteType]) -> bool { + left.len() == right.len() && left.iter().zip(right) + .all(|(left, right)|left.equivalent(right)) +} + +fn members_equivalent(left: &[DMLResolvedMember], + right: &[DMLResolvedMember]) -> bool { + left.len() == right.len() && left.iter().zip(right).all(|(left, right)| + left.name == right.name && left.typed.equivalent(&right.typed)) } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -62,12 +815,68 @@ impl DMLResolvedType { matches!(self, DMLResolvedType::Dummy(_)) } - pub fn equivalent(&self, _: &DMLResolvedType) -> bool { - // TODO: actual type comparison beyond this - // Currently, we are using this only to typecheck arguments - // for method overrides, so returning true avoids - // false negatives - true + pub fn equivalent(&self, other: &DMLResolvedType) -> bool { + match (self, other) { + (DMLResolvedType::Actual(left), DMLResolvedType::Actual(right)) => + left.equivalent(right), + _ => true, + } + } + + pub fn equivalent_unqualified(&self, other: &DMLResolvedType) -> bool { + self.equivalent_unqualified_with_mode(other, TypeComparisonMode::default()) + } + + pub fn equivalent_unqualified_with_mode( + &self, + other: &DMLResolvedType, + mode: TypeComparisonMode, + ) -> bool { + fn unqualified(typ: &DMLConcreteType) -> &DMLConcreteType { + match &typ.kind { + DMLConcreteTypeKind::Typedef { + underlying: Some(underlying), .. + } => unqualified(underlying), + DMLConcreteTypeKind::Const(inner) => unqualified(inner), + _ => typ, + } + } + + match (self, other) { + (DMLResolvedType::Actual(left), DMLResolvedType::Actual(right)) => { + let left = unqualified(left); + let right = unqualified(right); + match mode { + TypeComparisonMode::Strict => left.equivalent(right), + TypeComparisonMode::Fuzzy => left.equivalent_fuzzy(right), + } + }, + _ => true, + } + } + + pub fn can_store(&self, source: &DMLResolvedType) -> bool { + self.storage_compatibility(source).is_compatible() + } + + pub fn storage_compatibility( + &self, + source: &DMLResolvedType, + ) -> StorageCompatibility { + self.storage_compatibility_with_mode(source, TypeComparisonMode::default()) + } + + pub fn storage_compatibility_with_mode( + &self, + source: &DMLResolvedType, + mode: TypeComparisonMode, + ) -> StorageCompatibility { + match (self, source) { + (DMLResolvedType::Actual(target), + DMLResolvedType::Actual(source)) => + target.storage_compatibility_with_mode(source, mode), + _ => StorageCompatibility::Compatible, + } } } @@ -77,16 +886,619 @@ impl From for DMLResolvedType { } } -pub fn eval_type(ast: &DMLType, _location: (), _scope: (), +fn resolve_members(database: &TypeDatabase, + members: &[DMLTypeMember], + visited: &mut Vec, + anonymous: &mut Vec, + collect_anonymous: bool, + expression_type: &mut dyn FnMut( + &ExpressionKind) -> Option) + -> Vec { + members.iter().map(|member|DMLResolvedMember { + name: member.name.as_ref().map(|name|name.val.clone()), + typed: resolve_concrete_type( + database, &member.typed, visited, anonymous, collect_anonymous, + expression_type), + }).collect() +} + +fn resolve_concrete_type(database: &TypeDatabase, + ast: &DMLType, + visited: &mut Vec, + anonymous: &mut Vec, + collect_anonymous: bool, + expression_type: &mut dyn FnMut( + &ExpressionKind) -> Option) + -> DMLConcreteType { + use DMLConcreteTypeKind::*; + + let kind = match &ast.kind { + DMLTypeKind::Named(name) => { + let ids = database.typedefs_named(&name.val); + if let [id] = ids { + if let Some(declaration) = database.declaration(*id) { + if let Some(underlying) = &declaration.underlying { + if visited.contains(id) { + Typedef { id: *id, underlying: None } + } else { + visited.push(*id); + let resolved = resolve_concrete_type( + database, underlying, visited, anonymous, false, + expression_type); + visited.pop(); + Typedef { + id: *id, + underlying: Some(Box::new(resolved)), + } + } + } else { + Template(*id) + } + } else { + Named(name.val.clone()) + } + } else if is_builtin_type(&name.val) { + Named(name.val.clone()) + } else { + Unknown + } + }, + DMLTypeKind::Struct(members) => { + if collect_anonymous { + anonymous.push(DMLStructType { span: ast.span }); + } + Struct { + identity: ast.span, + fields: resolve_members( + database, members, visited, anonymous, + collect_anonymous, expression_type), + } + }, + DMLTypeKind::Layout { byte_order, fields } => Layout { + identity: ast.span, + byte_order: byte_order.clone(), + fields: resolve_members( + database, fields, visited, anonymous, + collect_anonymous, expression_type), + }, + DMLTypeKind::Bitfields { width, fields } => Bitfields { + identity: ast.span, + width: width.clone(), + fields: resolve_members(database, &fields.iter() + .map(|field|field.member.clone()).collect::>(), + visited, anonymous, collect_anonymous, expression_type), + }, + DMLTypeKind::TypeOf(expression) => expression_type(expression) + .map(|typed|typed.kind) + .unwrap_or(TypeOf), + DMLTypeKind::Sequence(name) => { + let template = database.typedefs_named(&name.val).iter() + .copied().find(|id|database.declaration(*id) + .is_some_and(|declaration|declaration.underlying.is_none())); + Sequence { template, name: name.val.clone() } + }, + DMLTypeKind::Hook(arguments) => Hook(arguments.iter().map(|argument| + resolve_concrete_type( + database, argument, visited, anonymous, collect_anonymous, + expression_type)).collect()), + DMLTypeKind::Const(inner) => Const(Box::new(resolve_concrete_type( + database, inner, visited, anonymous, collect_anonymous, + expression_type))), + DMLTypeKind::Pointer(inner) => Pointer(Box::new(resolve_concrete_type( + database, inner, visited, anonymous, collect_anonymous, + expression_type))), + DMLTypeKind::Vector(inner) => Vector(Box::new(resolve_concrete_type( + database, inner, visited, anonymous, collect_anonymous, + expression_type))), + DMLTypeKind::Array { element, size } => Array { + element: Box::new(resolve_concrete_type( + database, element, visited, anonymous, collect_anonymous, + expression_type)), + size: match size.as_deref() { + None => DMLArraySize::Unspecified, + Some(expression @ ExpressionKind::IntegerLiteral(integer)) => + u64::try_from(integer.value()) + .map(DMLArraySize::Constant) + .unwrap_or_else(|_|DMLArraySize::Expression( + Box::new(expression.clone()))), + Some(expression) => + DMLArraySize::Expression(Box::new(expression.clone())), + }, + }, + DMLTypeKind::Function { returns, parameters, variadic } => Function { + returns: Box::new(resolve_concrete_type( + database, returns, visited, anonymous, collect_anonymous, + expression_type)), + parameters: parameters.iter().map(|parameter| + resolve_concrete_type( + database, parameter, visited, anonymous, + collect_anonymous, + expression_type)).collect(), + variadic: *variadic, + }, + DMLTypeKind::Unknown => Unknown, + }; + DMLConcreteType { kind, span: ast.span } +} + +pub fn eval_type(ast: &DMLType, database: &TypeDatabase, _in_extern: bool, _typename_hint: Option<&str>, _allow_void: bool) -> (Vec, DMLConcreteType) { - (vec![], DMLConcreteType::BaseType(DMLBaseType { - // TODO: replace with .span once types are concrete - no_info: *ast })) + eval_type_with_expression(ast, database, |_|None) +} + +pub fn eval_type_with_expression( + ast: &DMLType, + database: &TypeDatabase, + mut expression_type: impl FnMut( + &ExpressionKind) -> Option, +) -> (Vec, DMLConcreteType) { + let mut anonymous = vec![]; + let resolved = resolve_concrete_type( + database, ast, &mut vec![], &mut anonymous, true, + &mut expression_type); + (anonymous, resolved) } -pub fn eval_type_simple(ast: &DMLType, location: (), scope: ()) +pub fn eval_type_simple(ast: &DMLType, database: &TypeDatabase) -> (Vec, DMLConcreteType) { - eval_type(ast, location, scope, false, None, false) + eval_type(ast, database, false, None, false) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn named_type(name: &str) -> DMLType { + let span = ZeroSpan::invalid("resolved-type-test.dml"); + DMLType { + kind: DMLTypeKind::Named(DMLString { + val: name.to_string(), + span, + }), + span, + } + } + + fn wrapped_type(kind: impl FnOnce(Box) -> DMLTypeKind, + inner: DMLType) -> DMLType { + DMLType { + span: inner.span, + kind: kind(Box::new(inner)), + } + } + + fn add_type(database: &mut TypeDatabase, + name: &str, + underlying: Option) -> TypeId { + let span = ZeroSpan::invalid("resolved-type-test.dml"); + let id = TypeId(database.declarations.len()); + database.declarations.push(TypeDeclaration { + id, + name: DMLString { val: name.to_string(), span }, + declaration_span: span, + underlying, + is_extern: false, + }); + database.typedefs_by_name.entry(name.to_string()) + .or_default().push(id); + id + } + + fn resolved(ast: &DMLType, database: &TypeDatabase) -> DMLResolvedType { + eval_type_simple(ast, database).1.into() + } + + fn array_type(element: DMLType, length: Option) -> DMLType { + let span = element.span; + DMLType { + span, + kind: DMLTypeKind::Array { + element: Box::new(element), + size: length.map(|value|Box::new( + ExpressionKind::IntegerLiteral( + crate::analysis::structure::expressions::IntegerLiteral::Signed( + crate::analysis::structure::expressions::Value { + val: value, + span, + })))), + }, + } + } + + #[test] + fn unknown_type_names_are_not_storage_checked() { + let database = TypeDatabase::default(); + let (_, builtin) = eval_type_simple(&named_type("uint2"), &database); + let (_, misspelled) = eval_type_simple(&named_type("uit2"), &database); + assert!(matches!(misspelled.kind, DMLConcreteTypeKind::Unknown)); + assert!(misspelled.can_store(&builtin)); + assert!(builtin.can_store(&misspelled)); + + let mut report = vec![]; + database.report_unknown_types(&named_type("uint2"), &mut report); + database.report_unknown_types( + &wrapped_type(DMLTypeKind::Pointer, named_type("uit2")), + &mut report); + assert_eq!(report.len(), 1); + assert_eq!(report[0].description, "Unknown type: 'uit2'"); + } + + #[test] + fn distinct_primitive_types_are_not_equivalent() { + let database = TypeDatabase::default(); + let (_, integer) = eval_type_simple(&named_type("int"), &database); + let (_, floating) = eval_type_simple(&named_type("float"), &database); + + assert!(!DMLResolvedType::from(integer) + .equivalent(&DMLResolvedType::from(floating))); + } + + #[test] + fn typedefs_are_transparent_but_templates_are_nominal() { + let mut database = TypeDatabase::default(); + add_type(&mut database, "integer_t", Some(named_type("int"))); + add_type(&mut database, "first_template", None); + add_type(&mut database, "second_template", None); + + assert!(resolved(&named_type("integer_t"), &database) + .equivalent(&resolved(&named_type("int"), &database))); + assert!(!resolved(&named_type("first_template"), &database) + .equivalent(&resolved(&named_type("second_template"), &database))); + } + + #[test] + fn pointer_and_array_types_are_distinct() { + let database = TypeDatabase::default(); + let pointer = wrapped_type(DMLTypeKind::Pointer, named_type("int")); + let array = array_type(named_type("int"), None); + + assert!(!resolved(&pointer, &database).equivalent(&resolved( + &array, &database))); + } + + #[test] + fn arrays_retain_literal_lengths() { + let database = TypeDatabase::default(); + let known = eval_type_simple( + &array_type(named_type("int"), Some(3)), &database).1; + let unknown = eval_type_simple( + &array_type(named_type("int"), None), &database).1; + let negative = eval_type_simple( + &array_type(named_type("int"), Some(-1)), &database).1; + let span = ZeroSpan::invalid("resolved-type-test.dml"); + let dynamic = eval_type_simple(&DMLType { + span, + kind: DMLTypeKind::Array { + element: Box::new(named_type("int")), + size: Some(Box::new(ExpressionKind::Identifier( + crate::analysis::structure::expressions::Identifier { + name: DMLString { + val: "dimension".to_string(), + span, + }, + }))), + }, + }, &database).1; + + assert!(matches!(known.kind, + DMLConcreteTypeKind::Array { + size: DMLArraySize::Constant(3), .. + })); + assert!(matches!(unknown.kind, + DMLConcreteTypeKind::Array { + size: DMLArraySize::Unspecified, .. + })); + assert!(matches!(negative.kind, + DMLConcreteTypeKind::Array { + size: DMLArraySize::Expression(_), .. + })); + assert!(matches!(dynamic.kind, + DMLConcreteTypeKind::Array { + size: DMLArraySize::Expression(_), .. + })); + } + + #[test] + fn array_lengths_participate_in_equivalence() { + let database = TypeDatabase::default(); + let three = resolved( + &array_type(named_type("int"), Some(3)), &database); + let another_three = resolved( + &array_type(named_type("int"), Some(3)), &database); + let four = resolved( + &array_type(named_type("int"), Some(4)), &database); + let unknown = resolved( + &array_type(named_type("int"), None), &database); + let negative = resolved( + &array_type(named_type("int"), Some(-1)), &database); + let another_negative = resolved( + &array_type(named_type("int"), Some(-1)), &database); + + assert!(three.equivalent(&another_three)); + assert!(!three.equivalent(&four)); + assert!(!three.equivalent(&unknown)); + assert!(!negative.equivalent(&unknown)); + assert!(negative.equivalent(&another_negative)); + } + + #[test] + fn recursive_typedef_resolution_terminates() { + let mut database = TypeDatabase::default(); + let id = add_type(&mut database, "node_t", Some(wrapped_type( + DMLTypeKind::Pointer, named_type("node_t")))); + + let node = resolved(&named_type("node_t"), &database); + assert!(node.equivalent(&node)); + let recursion_marker = DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Typedef { + id, + underlying: None, + }, + span: ZeroSpan::invalid("resolved-type-test.dml"), + }); + assert!(node.equivalent(&recursion_marker)); + assert!(recursion_marker.equivalent(&node)); + } + + #[test] + fn distinct_struct_declarations_are_nominal() { + let database = TypeDatabase::default(); + let first_span = ZeroSpan::invalid("first-struct.dml"); + let second_span = ZeroSpan::invalid("second-struct.dml"); + let first = DMLType { + kind: DMLTypeKind::Struct(vec![]), + span: first_span, + }; + let second = DMLType { + kind: DMLTypeKind::Struct(vec![]), + span: second_span, + }; + + assert!(!resolved(&first, &database) + .equivalent(&resolved(&second, &database))); + } + + #[test] + fn struct_equivalence_uses_declaration_identity() { + let span = ZeroSpan::invalid("same-struct.dml"); + let first = DMLConcreteType { + kind: DMLConcreteTypeKind::Struct { + identity: span, + fields: vec![], + }, + span, + }; + let second = DMLConcreteType { + kind: DMLConcreteTypeKind::Struct { + identity: span, + fields: vec![DMLResolvedMember { + name: Some("field".to_string()), + typed: DMLConcreteType { + kind: DMLConcreteTypeKind::Named("int".to_string()), + span, + }, + }], + }, + span, + }; + + assert!(first.equivalent(&second)); + assert!(second.equivalent(&first)); + } + + #[test] + fn arithmetic_types_can_store_each_other() { + let database = TypeDatabase::default(); + + assert!(resolved(&named_type("int"), &database) + .can_store(&resolved(&named_type("uint32"), &database))); + assert!(resolved(&named_type("float"), &database) + .can_store(&resolved(&named_type("int"), &database))); + assert!(!resolved(&named_type("bool"), &database) + .can_store(&resolved(&named_type("int"), &database))); + assert!(resolved(&named_type("bool"), &database) + .can_store(&resolved(&named_type("uint1"), &database))); + assert!(resolved(&named_type("uint1"), &database) + .can_store(&resolved(&named_type("bool"), &database))); + assert!(!resolved(&named_type("uint8"), &database) + .can_store(&resolved(&named_type("bool"), &database))); + } + + #[test] + fn bitfields_store_integers_like_their_unsigned_backing_type() { + let database = TypeDatabase::default(); + let span = ZeroSpan::invalid("bitfield-storage-test.dml"); + let bitfields = DMLResolvedType::Actual(DMLConcreteType { + kind: DMLConcreteTypeKind::Bitfields { + identity: span, + width: Some("8".to_string()), + fields: vec![], + }, + span, + }); + let uint8 = resolved(&named_type("uint8"), &database); + let uint16 = resolved(&named_type("uint16"), &database); + let floating = resolved(&named_type("double"), &database); + + assert_eq!(bitfields.storage_compatibility(&uint8), + StorageCompatibility::Compatible); + assert_eq!(bitfields.storage_compatibility(&uint16), + StorageCompatibility::Truncates( + IntegerTruncation::AllTargets)); + assert_eq!(bitfields.storage_compatibility(&floating), + StorageCompatibility::Incompatible); + assert_eq!(uint8.storage_compatibility(&bitfields), + StorageCompatibility::Compatible); + } + + #[test] + fn integer_storage_reports_platform_specific_truncation() { + let database = TypeDatabase::default(); + let compatibility = |target: &str, source: &str|resolved( + &named_type(target), &database).storage_compatibility( + &resolved(&named_type(source), &database)); + + assert_eq!(compatibility("uint8", "uint64"), + StorageCompatibility::Truncates( + IntegerTruncation::AllTargets)); + assert_eq!(compatibility("long", "uint64"), + StorageCompatibility::Truncates( + IntegerTruncation::Windows)); + assert_eq!(compatibility("int", "long"), + StorageCompatibility::Truncates( + IntegerTruncation::NonWindows)); + assert_eq!(compatibility("uint64", "long"), + StorageCompatibility::Compatible); + } + + #[test] + fn integer_storage_matches_dmlc_signedness_rules() { + let database = TypeDatabase::default(); + let compatibility = |target: &str, source: &str|resolved( + &named_type(target), &database).storage_compatibility( + &resolved(&named_type(source), &database)); + + assert_eq!(compatibility("int8", "uint8"), + StorageCompatibility::Compatible); + assert_eq!(compatibility("uint8", "int8"), + StorageCompatibility::Compatible); + assert_eq!(compatibility("int64", "uint64"), + StorageCompatibility::Compatible); + assert_eq!(compatibility("uint64", "int64"), + StorageCompatibility::Compatible); + assert_eq!(compatibility("int16", "uint8"), + StorageCompatibility::Compatible); + assert_eq!(compatibility("uint8", "int16"), + StorageCompatibility::Truncates( + IntegerTruncation::AllTargets)); + } + + #[test] + fn integer_literal_truncation_uses_value_and_signedness() { + let database = TypeDatabase::default(); + let actual = |name: &str|match resolved(&named_type(name), &database) { + DMLResolvedType::Actual(typed) => typed, + DMLResolvedType::Dummy(_) => unreachable!(), + }; + let uint8 = actual("uint8"); + let long = actual("long"); + + assert_eq!(uint8.integer_value_truncation(255), None); + assert_eq!(uint8.integer_value_truncation(256), + Some(IntegerTruncation::AllTargets)); + assert_eq!(uint8.integer_value_truncation(-1), + Some(IntegerTruncation::AllTargets)); + assert_eq!(long.integer_value_truncation(i64::MAX.into()), + Some(IntegerTruncation::Windows)); + } + + #[test] + fn storage_check_uses_integer_values_when_available() { + let database = TypeDatabase::default(); + let actual = |name: &str|match resolved(&named_type(name), &database) { + DMLResolvedType::Actual(typed) => typed, + DMLResolvedType::Dummy(_) => unreachable!(), + }; + let target = actual("uint8"); + let source = actual("uint64"); + + assert_eq!(check_storage_compatibility( + &target, &source, Some(StorageValue::Integer(255))), + StorageCompatibility::Compatible); + assert_eq!(check_storage_compatibility( + &target, &source, Some(StorageValue::Integer(256))), + StorageCompatibility::Truncates(IntegerTruncation::AllTargets)); + assert_eq!(check_storage_compatibility(&target, &source, None), + StorageCompatibility::Truncates(IntegerTruncation::AllTargets)); + } + + #[test] + fn can_store_is_transparent_through_typedefs() { + let mut database = TypeDatabase::default(); + add_type(&mut database, "integer_t", Some(named_type("int"))); + + assert!(resolved(&named_type("integer_t"), &database) + .can_store(&resolved(&named_type("uint32"), &database))); + } + + #[test] + fn pointers_can_add_but_not_discard_pointee_const() { + let database = TypeDatabase::default(); + let int_pointer = wrapped_type( + DMLTypeKind::Pointer, named_type("int")); + let const_int_pointer = wrapped_type( + DMLTypeKind::Pointer, + wrapped_type(DMLTypeKind::Const, named_type("int"))); + + assert!(resolved(&const_int_pointer, &database) + .can_store(&resolved(&int_pointer, &database))); + assert!(!resolved(&int_pointer, &database) + .can_store(&resolved(&const_int_pointer, &database))); + } + + #[test] + fn comparison_modes_match_dmlc_pointer_semantics() { + let database = TypeDatabase::default(); + let int_pointer = resolved(&wrapped_type( + DMLTypeKind::Pointer, named_type("int")), &database); + let int_array = resolved(&array_type(named_type("int"), Some(4)), + &database); + let const_int_pointer = resolved(&wrapped_type( + DMLTypeKind::Pointer, + wrapped_type(DMLTypeKind::Const, named_type("int"))), + &database); + let void_pointer = resolved(&wrapped_type( + DMLTypeKind::Pointer, named_type("void")), &database); + + assert!(int_pointer.equivalent_unqualified(&int_array)); + assert!(!int_pointer.equivalent_unqualified_with_mode( + &int_array, TypeComparisonMode::Strict)); + assert!(int_pointer.equivalent_unqualified(&const_int_pointer)); + assert!(!int_pointer.equivalent_unqualified_with_mode( + &const_int_pointer, TypeComparisonMode::Strict)); + assert_eq!(void_pointer.storage_compatibility(&const_int_pointer), + StorageCompatibility::Compatible); + assert_eq!(void_pointer.storage_compatibility_with_mode( + &const_int_pointer, TypeComparisonMode::Strict), + StorageCompatibility::Incompatible); + } + + #[test] + fn pointer_targets_accept_array_decay_and_void_pointers() { + let database = TypeDatabase::default(); + let int_pointer = wrapped_type(DMLTypeKind::Pointer, named_type("int")); + let int_array = array_type(named_type("int"), None); + let void_pointer = wrapped_type(DMLTypeKind::Pointer, named_type("void")); + + assert!(resolved(&int_pointer, &database) + .can_store(&resolved(&int_array, &database))); + assert!(resolved(&void_pointer, &database) + .can_store(&resolved(&int_pointer, &database))); + } + + #[test] + fn can_store_rejects_incompatible_wrappers() { + let database = TypeDatabase::default(); + let pointer = wrapped_type(DMLTypeKind::Pointer, named_type("int")); + let vector = wrapped_type(DMLTypeKind::Vector, named_type("int")); + + assert!(!resolved(&pointer, &database) + .can_store(&resolved(&vector, &database))); + } + + #[test] + fn unresolved_types_do_not_cascade_storage_errors() { + let database = TypeDatabase::default(); + let unknown = DMLType { + kind: DMLTypeKind::Unknown, + span: ZeroSpan::invalid("resolved-type-test.dml"), + }; + + assert!(resolved(&unknown, &database) + .can_store(&resolved(&named_type("int"), &database))); + assert!(resolved(&named_type("int"), &database) + .can_store(&resolved(&unknown, &database))); + } } diff --git a/src/dfa/client.rs b/src/dfa/client.rs index 8406ab26..23b8875d 100644 --- a/src/dfa/client.rs +++ b/src/dfa/client.rs @@ -1,7 +1,7 @@ // © 2024 Intel Corporation // SPDX-License-Identifier: Apache-2.0 and MIT use core::time::Duration; -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeMap, HashSet}; use std::error::Error; use std::fmt::{self, Display, Formatter}; use std::fs; @@ -100,7 +100,7 @@ pub struct ClientInterface { server: Job, reader: channel::Receiver, _reading_thread: JoinHandle<()>, - diagnostics: HashMap>, + diagnostics: BTreeMap>, waiting_for_received_diag: HashSet, waiting_for_received_lint: HashSet, linting_enabled: bool, @@ -140,7 +140,7 @@ impl ClientInterface { server, reader: receiver, _reading_thread: reading_thread, - diagnostics: HashMap::default(), + diagnostics: BTreeMap::default(), waiting_for_received_diag: HashSet::default(), waiting_for_received_lint: HashSet::default(), linting_enabled: lint, diff --git a/src/server/dispatch.rs b/src/server/dispatch.rs index 5e4ee43e..d787895d 100644 --- a/src/server/dispatch.rs +++ b/src/server/dispatch.rs @@ -98,6 +98,7 @@ macro_rules! define_dispatch_request_enum { define_dispatch_request_enum!( Completion, GotoDefinition, + GotoTypeDefinition, References, DocumentSymbolRequest, WorkspaceSymbolRequest, diff --git a/src/server/mod.rs b/src/server/mod.rs index 0859a099..2f4beae5 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -41,6 +41,7 @@ use lsp_types::{ TextDocumentSyncOptions, TextDocumentSyncKind, TextDocumentSyncSaveOptions, + TypeDefinitionProviderCapability, WorkspaceServerCapabilities, WorkspaceFoldersServerCapabilities, }; @@ -597,6 +598,7 @@ impl LsService { requests::DocumentSymbolRequest, requests::HoverRequest, requests::GotoDefinition, + requests::GotoTypeDefinition, requests::GotoDeclaration, requests::References, requests::Completion, @@ -709,7 +711,8 @@ fn server_caps(_ctx: &ActionContext) -> ServerCapabilities { hover_provider: Some(HoverProviderCapability::Simple(true)), completion_provider: None, definition_provider: Some(OneOf::Left(true)), - type_definition_provider: None, + type_definition_provider: Some( + TypeDefinitionProviderCapability::Simple(true)), implementation_provider: Some( ImplementationProviderCapability::Simple(true)), references_provider: None, diff --git a/tests/common/parser_runner.rs b/tests/common/parser_runner.rs new file mode 100644 index 00000000..f631edb6 --- /dev/null +++ b/tests/common/parser_runner.rs @@ -0,0 +1,117 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +use std::io::{Read, Write}; +use std::path::PathBuf; +use std::process::{Command, Stdio}; +use std::thread; +use std::time::{Duration, Instant}; + +const CHILD_POLL_INTERVAL: Duration = Duration::from_millis(5); + +fn runner_exe() -> PathBuf { + PathBuf::from(env!("CARGO_BIN_EXE_dml-fuzz-runner")) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Outcome { + Ok, + Panic, + Timeout, +} + +/// Spawn the parser runner as a child so hangs and process-level failures do +/// not take down the integration-test process. +pub(crate) fn run_with_timeout(input: &str, timeout: Duration) + -> (Outcome, Option, Duration) { + let started = Instant::now(); + + let mut child = match Command::new(runner_exe()) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + { + Ok(c) => c, + Err(e) => { + return (Outcome::Panic, + Some(format!("failed to spawn parser runner: {e}")), + Duration::ZERO); + } + }; + + let mut stdin = child.stdin.take().expect("piped"); + let input_bytes = input.as_bytes().to_vec(); + let writer = thread::spawn(move || { + let _ = stdin.write_all(&input_bytes); + }); + + let deadline = started + timeout; + let status = loop { + match child.try_wait() { + Ok(Some(status)) => break Some(status), + Ok(None) => {} + Err(e) => { + let _ = child.kill(); + let _ = child.wait(); + let _ = writer.join(); + return (Outcome::Panic, + Some(format!("wait on parser runner failed: {e}")), + started.elapsed()); + } + } + if Instant::now() >= deadline { + break None; + } + thread::sleep(CHILD_POLL_INTERVAL); + }; + + let elapsed = started.elapsed(); + let status = match status { + Some(status) => status, + None => { + let _ = child.kill(); + let _ = child.wait(); + let _ = writer.join(); + return (Outcome::Timeout, None, timeout); + } + }; + + let mut stderr = String::new(); + if let Some(mut stream) = child.stderr.take() { + let _ = stream.read_to_string(&mut stderr); + } + let _ = writer.join(); + + if status.success() { + return (Outcome::Ok, None, elapsed); + } + + #[cfg(unix)] + { + use std::os::unix::process::ExitStatusExt; + if let Some(signal) = status.signal() { + let hint = match signal { + 11 => " (SIGSEGV; memory unsafety or guard-page hit)", + 6 => " (SIGABRT; Rust abort — stack overflow handler, assertion, or explicit abort)", + _ => "", + }; + return (Outcome::Panic, + Some(format!("killed by signal {signal}{hint}")), + elapsed); + } + } + + let message = stderr + .lines() + .find(|line| line.starts_with("PANIC: ")) + .map(|line| line["PANIC: ".len()..].to_owned()) + .unwrap_or_else(|| { + if stderr.is_empty() { + format!("exit code {}", status.code().unwrap_or(-1)) + } else { + stderr.trim().to_owned() + } + }); + (Outcome::Panic, Some(message), elapsed) +} diff --git a/tests/fuzzing.rs b/tests/fuzzing.rs index c0230343..ec2d9cc0 100644 --- a/tests/fuzzing.rs +++ b/tests/fuzzing.rs @@ -12,16 +12,18 @@ //! * `DLS_FUZZ_ITERS` — number of generated inputs per test (default 32). //! * `DLS_FUZZ_SEED` — PRNG seed (defaults to timestamp-based). -use std::io::{Read, Write}; use std::path::PathBuf; -use std::process::{Command, Stdio}; -use std::thread; -use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use rand::{RngExt, SeedableRng}; use rand::rngs::StdRng; use rand::seq::IndexedRandom; +#[path = "common/parser_runner.rs"] +mod parser_runner; + +use parser_runner::{Outcome, run_with_timeout}; + /// Per-input wall-clock budget. const PARSE_TIMEOUT: Duration = Duration::from_secs(60); @@ -37,133 +39,6 @@ const PANIC_PROBE_FLOOR: Duration = Duration::from_millis(100); /// Upper bound on bytes per generated input const MAX_INPUT_BYTES: usize = 64 * 1024; -/// How often to poll the child while waiting for it to finish. 5 ms is -/// negligible overhead next to the `PANIC_PROBE_FLOOR` (100 ms) and gives -/// us reasonably tight timeout enforcement. -const CHILD_POLL_INTERVAL: Duration = Duration::from_millis(5); - -/// Path to the `dml-fuzz-runner` binary. Cargo sets `CARGO_BIN_EXE_` -/// at compile time for integration tests and guarantees the bin is built -/// (with its `required-features`) before the test runs. -fn runner_exe() -> PathBuf { - PathBuf::from(env!("CARGO_BIN_EXE_dml-fuzz-runner")) -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Outcome { - Ok, - Panic, - Timeout, -} - -/// Spawn the fuzz-runner subprocess on `input`, kill it after `timeout`, -/// and classify the exit status. -/// -/// Running the parser in a child process (rather than a worker thread) -/// isolates uncatchable failures from the test process: a stack overflow -/// or any other signal-induced abort in the parser only kills the child -/// and is reported back as a `Panic` outcome with a signal note. The -/// caller's `cargo test` invocation keeps running. -fn run_with_timeout(input: &str, timeout: Duration) - -> (Outcome, Option, Duration) { - let started = Instant::now(); - - let mut child = match Command::new(runner_exe()) - .stdin(Stdio::piped()) - .stdout(Stdio::null()) - .stderr(Stdio::piped()) - .spawn() - { - Ok(c) => c, - Err(e) => { - return (Outcome::Panic, - Some(format!("failed to spawn fuzz runner: {e}")), - Duration::ZERO); - } - }; - - // Write stdin from a helper thread so a large input can't deadlock - // against the runner reading it. - let mut stdin = child.stdin.take().expect("piped"); - let input_bytes = input.as_bytes().to_vec(); - let writer = thread::spawn(move || { - let _ = stdin.write_all(&input_bytes); - // Dropping stdin closes the pipe so the runner's read_to_string returns. - }); - - // Poll until the child exits or we hit the deadline. - let deadline = started + timeout; - let status = loop { - match child.try_wait() { - Ok(Some(status)) => break Some(status), - Ok(None) => {} - Err(e) => { - let _ = child.kill(); - let _ = child.wait(); - let _ = writer.join(); - return (Outcome::Panic, - Some(format!("wait on fuzz runner failed: {e}")), - started.elapsed()); - } - } - if Instant::now() >= deadline { - break None; - } - thread::sleep(CHILD_POLL_INTERVAL); - }; - - let elapsed = started.elapsed(); - - let status = match status { - Some(s) => s, - None => { - let _ = child.kill(); - let _ = child.wait(); - let _ = writer.join(); - return (Outcome::Timeout, None, timeout); - } - }; - - // Drain stderr (small: just our "PANIC: ..." line on panic, empty on success). - let mut stderr = String::new(); - if let Some(mut s) = child.stderr.take() { - let _ = s.read_to_string(&mut stderr); - } - let _ = writer.join(); - - if status.success() { - return (Outcome::Ok, None, elapsed); - } - - #[cfg(unix)] - { - use std::os::unix::process::ExitStatusExt; - if let Some(sig) = status.signal() { - let hint = match sig { - 11 => " (SIGSEGV; memory unsafety or guard-page hit)", - 6 => " (SIGABRT; Rust abort — stack overflow handler, assertion, or explicit abort)", - _ => "", - }; - return (Outcome::Panic, - Some(format!("killed by signal {sig}{hint}")), - elapsed); - } - } - - let msg = stderr - .lines() - .find(|l| l.starts_with("PANIC: ")) - .map(|l| l["PANIC: ".len()..].to_owned()) - .unwrap_or_else(|| { - if stderr.is_empty() { - format!("exit code {}", status.code().unwrap_or(-1)) - } else { - stderr.trim().to_owned() - } - }); - (Outcome::Panic, Some(msg), elapsed) -} - /// The parser only accepts `&str`, so any bytes that aren't valid UTF-8 are /// replaced with U+FFFD via lossy conversion fn normalize(input: &[u8]) -> String { diff --git a/tests/lsp_lookup_tests.rs b/tests/lsp_lookup_tests.rs index 83f87fed..86d37a1e 100644 --- a/tests/lsp_lookup_tests.rs +++ b/tests/lsp_lookup_tests.rs @@ -11,6 +11,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::LazyLock; use regex::Regex; +use lsp_types::DiagnosticSeverity; use dls::actions::analysis_storage::{AnalysisStorage, TimestampedStorage, AnalysisLookupError}; use dls::actions::{InitActionContext, DeviceAnalysisJobOptions}; @@ -23,7 +24,8 @@ use dls::span::{Position, ZeroIndexed}; use dls::file_management::{CanonPath, PathResolver}; use dls::server::io::Output; use dls::actions::semantic_lookup::{definitions_at_fp, declarations_at_fp, - implementations_at_fp, references_at_fp, DLSLimitation}; + implementations_at_fp, references_at_fp, + type_definitions_at_fp, DLSLimitation}; // Mock output for testing #[derive(Clone, Debug)] @@ -64,6 +66,7 @@ enum OperationType { GotoDeclaration, // @goto-decl -> name GotoImplementation, // @goto-impl -> name,name,... FindReferences, // @goto-ref -> name,name,... + GotoTypeDefinition, // @goto-type-def -> name } impl std::fmt::Display for OperationType { @@ -73,6 +76,7 @@ impl std::fmt::Display for OperationType { OperationType::GotoDeclaration => write!(f, "goto-decl"), OperationType::GotoImplementation => write!(f, "goto-impl"), OperationType::FindReferences => write!(f, "goto-ref"), + OperationType::GotoTypeDefinition => write!(f, "goto-type-def"), } } } @@ -171,6 +175,23 @@ struct Annotation { type UnresolvedAnnotation = Annotation; type ResolvedAnnotation = Annotation; +#[derive(Debug, Clone)] +struct DiagnosticRelated { + target: T, + description: String, +} + +#[derive(Debug, Clone)] +struct DiagnosticAnnotation { + location: SourceLocation, + severity: DiagnosticSeverity, + description: String, + related: Vec>, +} + +type UnresolvedDiagnosticAnnotation = DiagnosticAnnotation; +type ResolvedDiagnosticAnnotation = DiagnosticAnnotation; + /// Look up the source token at a position by walking the parse tree. fn token_at_position( analysis: &Arc>, @@ -197,13 +218,13 @@ static RE_LOC: LazyLock = LazyLock::new(|| { /// Regex for operation annotations static RE_OP: LazyLock = LazyLock::new(|| { - Regex::new(r"@(goto-def-decl|goto-def|goto-decl|goto-impl|goto-ref)\[(\d+)\]->([^@]*)").unwrap() + Regex::new(r"@(goto-def-decl|goto-def|goto-decl|goto-impl|goto-ref|goto-type-def)\[(\d+)\]->([^@]*)").unwrap() }); /// Regex for an operation annotation that is missing the required column bracket. /// Used for detecting test-writer errors static RE_OP_NO_COL: LazyLock = LazyLock::new(|| { - Regex::new(r"@(goto-def-decl|goto-def|goto-decl|goto-impl|goto-ref)->").unwrap() + Regex::new(r"@(goto-def-decl|goto-def|goto-decl|goto-impl|goto-ref|goto-type-def)->").unwrap() }); /// Catch-all regex for any `@word[...]->` or `@word->` pattern. @@ -212,6 +233,18 @@ static RE_UNKNOWN_OP: LazyLock = LazyLock::new(|| { Regex::new(r"@([a-zA-Z][-a-zA-Z0-9]*)(\[[^\]]*\])?->").unwrap() }); +/// Regex for `@diag-error: description | related->location: description, ...`. +static RE_DIAGNOSTIC: LazyLock = LazyLock::new(|| { + Regex::new(r"@diag-(error|warning|information|hint):\s*(.+)$").unwrap() +}); + +/// Regex for a related-information clause within an `@diag` annotation. +static RE_DIAGNOSTIC_RELATED: LazyLock = LazyLock::new(|| { + Regex::new( + r"^related->([A-Za-z0-9_.-]+(?::[A-Za-z_][A-Za-z0-9_]*)?):\s*(.+)$" + ).unwrap() +}); + /// Parse annotations from a DML test file /// /// Annotation format: @@ -223,6 +256,12 @@ static RE_UNKNOWN_OP: LazyLock = LazyLock::new(|| { /// - @goto-impl[col]->name - Tests goto-implementation at column, expecting it to resolve to 'name' /// - @goto-ref[col]->name1,name2 - Tests find-references at column, expecting multiple results /// - @goto-def[col]-> - Tests that lookup returns no results (empty target list) +/// - @diag-error: text - Expects an error on the next code line containing `text` +/// - @diag-warning: text - Expects a warning on the next code line containing `text` +/// - @diag-information: text - Expects an information diagnostic on the next code line +/// - @diag-hint: text - Expects a hint diagnostic on the next code line +/// - @diag-error: text | related->name: related text, ... - Also expects exact related +/// information whose spans contain the named locations and messages contain the texts /// /// Negated targets: /// - ..., !name, ... - Asserts that the location specified by name is NOT in the lookup results, @@ -238,9 +277,12 @@ static RE_UNKNOWN_OP: LazyLock = LazyLock::new(|| { /// Cross-file references: /// - @goto-def[col]->file.dml:name - References a location in another file #[track_caller] -fn parse_annotations(content: &str, file_path: Option) -> (Vec, Vec) { +fn parse_annotations(content: &str, file_path: Option) + -> (Vec, Vec, + Vec) { let mut locations = Vec::new(); let mut operations = Vec::new(); + let mut diagnostics: Vec = Vec::new(); let lines: Vec<&str> = content.lines().collect(); // Build a map to find the next non-annotated code line for any given line, used @@ -290,6 +332,7 @@ fn parse_annotations(content: &str, file_path: Option) -> (Vec vec![OperationType::GotoDefinition, OperationType::GotoDeclaration], "goto-impl" => vec![OperationType::GotoImplementation], "goto-ref" => vec![OperationType::FindReferences], + "goto-type-def" => vec![OperationType::GotoTypeDefinition], other => unreachable!("regex does not match '{}'", other), }; let col: u32 = cap[2].parse() @@ -311,15 +354,56 @@ fn parse_annotations(content: &str, file_path: Option) -> (Vec DiagnosticSeverity::ERROR, + "warning" => DiagnosticSeverity::WARNING, + "information" => DiagnosticSeverity::INFORMATION, + "hint" => DiagnosticSeverity::HINT, + other => unreachable!("regex does not match '{}'", other), + }; + let mut parts = cap[2].splitn(2, '|'); + let description = parts.next().unwrap().trim(); + assert!(!description.is_empty(), + "@diag description must not be empty on line {}", line_num + 1); + let related = parts.next().map(|clauses|clauses.split(',').map(|clause| { + let clause = clause.trim(); + let related = RE_DIAGNOSTIC_RELATED.captures(clause) + .unwrap_or_else(||panic!( + "invalid @diag related clause '{}' on line {}; expected related->loc_tag: message", + clause, line_num + 1)); + let targets = parse_target_list(&related[1], line_num + 1, line); + let [target] = targets.as_slice() else { + panic!("@diag related clause requires exactly one @loc target on line {}: {}", + line_num + 1, line.trim()); + }; + assert_eq!(target.kind, TargetKind::Positive, + "@diag related target must not be negated or questionable on line {}", + line_num + 1); + DiagnosticRelated { + target: target.clone(), + description: related[2].trim().to_string(), + } + }).collect()).unwrap_or_default(); + diagnostics.push(DiagnosticAnnotation { + location: SourceLocation::new( + effective_line, 0, file_path.clone()), + severity, + description: description.to_string(), + related, + }); + } + // Check for any unrecognized @...-> patterns (likely typos) for cap in RE_UNKNOWN_OP.captures_iter(line) { let tag = &cap[1]; let known = matches!(tag, - "goto-def" | "goto-decl" | "goto-def-decl" | "goto-impl" | "goto-ref"); + "goto-def" | "goto-decl" | "goto-def-decl" | "goto-impl" | + "goto-ref" | "goto-type-def"); if !known { panic!( "unrecognized annotation '@{}' on line {}, \ - expected one of: @goto-def, @goto-decl, @goto-def-decl, @goto-impl, @goto-ref\n \ + expected one of: @goto-def, @goto-decl, @goto-def-decl, @goto-impl, @goto-ref, @goto-type-def\n \ line {}: {}", tag, line_num + 1, line_num + 1, line.trim() ); @@ -327,7 +411,7 @@ fn parse_annotations(content: &str, file_path: Option) -> (Vec, + name_map: &HashMap>, +) -> Vec { + unresolved.into_iter().map(|annotation|DiagnosticAnnotation { + location: annotation.location, + severity: annotation.severity, + description: annotation.description, + related: annotation.related.into_iter().map(|related| { + let key = related.target.key(); + let &position = name_map.get(&key).unwrap_or_else(||panic!( + "Diagnostic annotation references unknown @loc target '{}'", + key)); + DiagnosticRelated { + target: ResolvedTarget { + spec: related.target, + position, + }, + description: related.description, + } + }).collect(), + }).collect() +} + /// Build a map from each line to the next non-comment, non-empty code line. fn build_next_code_line_map(lines: &[&str]) -> Vec> { let mut result = vec![None; lines.len()]; @@ -521,6 +630,8 @@ struct TestSetup { main_file: PathBuf, main_canon_path: CanonPath, annotations: Vec, + diagnostics: Vec, + fixture_paths: Vec, } impl std::fmt::Debug for TestSetup { @@ -529,6 +640,8 @@ impl std::fmt::Debug for TestSetup { .field("main_file", &self.main_file) .field("main_canon_path", &self.main_canon_path.as_path()) .field("annotations", &self.annotations.len()) + .field("diagnostics", &self.diagnostics.len()) + .field("fixture_paths", &self.fixture_paths) .finish_non_exhaustive() } } @@ -539,6 +652,7 @@ struct LoadedFile { filename: String, locations: Vec, operations: Vec, + diagnostics: Vec, is_device: bool, } @@ -554,11 +668,13 @@ fn collect_file_annotations( filename: &str, canon_path: &CanonPath, analysis: &Arc>, -) -> (Vec, Vec) { +) -> (Vec, Vec, + Vec) { let content = std::fs::read_to_string(file_path) .unwrap_or_else(|e| panic!("Failed to read {}: {}", filename, e)); - let (mut locations, mut operations) = parse_annotations(&content, Some(canon_path.to_path_buf())); + let (mut locations, mut operations, mut diagnostics) = + parse_annotations(&content, Some(canon_path.to_path_buf())); // Prefix @loc names with the declaring filename. for loc in &mut locations { @@ -575,6 +691,14 @@ fn collect_file_annotations( } } + for diagnostic in &mut diagnostics { + for related in &mut diagnostic.related { + if related.target.filename.is_none() { + related.target.filename = Some(filename.to_string()); + } + } + } + // Resolve display tokens against the AST now that the file has been // analyzed in isolation. This lets cursors land in the middle of an // identifier and still get the full identifier text in test output. @@ -583,7 +707,7 @@ fn collect_file_annotations( op.token = token_at_position(analysis, canon_path, &file, op.location.position); } - (locations, operations) + (locations, operations, diagnostics) } // Helper function to build a device analysis based on isolated analysis available within a storage @@ -649,9 +773,12 @@ fn setup_test_with_imports( let loaded_files: Vec = filenames.iter().map(|filename| { let file_path = base_dir.join(filename); let canon = load_isolated_analysis(&analysis, &vfs, &file_path); - verify_no_parse_errors(&analysis, &canon, filename); - let (locations, operations) = + let (locations, operations, diagnostics) = collect_file_annotations(&file_path, filename, &canon, &analysis); + // Files with diagnostic annotations assert their parse errors instead + if diagnostics.is_empty() { + verify_no_parse_errors(&analysis, &canon, filename); + } let is_device = analysis.lock().unwrap() .get_isolated_analysis(&canon) .expect("isolated analysis must exist for just-loaded file") @@ -661,6 +788,7 @@ fn setup_test_with_imports( filename: filename.to_string(), locations, operations, + diagnostics, is_device, } }).collect(); @@ -691,25 +819,40 @@ fn setup_test_with_imports( let main_file = base_dir.join(filenames[0]); let main_canon = loaded_files[0].canon_path.clone(); + let fixture_paths = loaded_files.iter() + .map(|file|file.canon_path.to_path_buf()) + .collect(); let all_locations: Vec<_> = loaded_files.iter() .flat_map(|f| &f.locations) .cloned() .collect(); - let all_operations: Vec<_> = loaded_files.into_iter() - .flat_map(|f| f.operations) + let all_operations: Vec<_> = loaded_files.iter() + .flat_map(|f| f.operations.iter().cloned()) + .collect(); + let all_diagnostics: Vec<_> = loaded_files.iter() + .flat_map(|f| f.diagnostics.iter().cloned()) .collect(); let name_map = build_name_mapping(&all_locations); let annotations = resolve_annotations(all_operations, &name_map); - assert_all_locs_used(&all_locations, &annotations); + let diagnostics = resolve_diagnostic_annotations(all_diagnostics, &name_map); + assert_all_locs_used(&all_locations, &annotations, &diagnostics); let ctx: InitActionContext = create_test_init_context( Arc::clone(&analysis), Arc::clone(&vfs), ); - TestSetup { ctx, analysis, main_file, main_canon_path: main_canon, annotations } + TestSetup { + ctx, + analysis, + main_file, + main_canon_path: main_canon, + annotations, + diagnostics, + fixture_paths, + } } /// Verify every `@loc` declaration is referenced by at least one annotation @@ -719,10 +862,13 @@ fn setup_test_with_imports( fn assert_all_locs_used( locations: &[LocationDeclaration], annotations: &[ResolvedAnnotation], + diagnostics: &[ResolvedDiagnosticAnnotation], ) { - let used: HashSet = annotations.iter() + let mut used: HashSet = annotations.iter() .flat_map(|ann| ann.targets.iter().map(|t| t.spec.key())) .collect(); + used.extend(diagnostics.iter().flat_map(|diagnostic|diagnostic.related.iter()) + .map(|related|related.target.spec.key())); let mut unused: Vec<&LocationDeclaration> = locations.iter() .filter(|loc| !used.contains(&loc.name)) .collect(); @@ -761,6 +907,29 @@ mod tests { let _ = env_logger::try_init(); } + #[test] + fn duplicate_templates_keep_earliest_span() { + init_logging(); + for _ in 0..8 { + let setup = setup_test(&["duplicate_template_device.dml", + "duplicate_template_import.dml"]); + let analysis = setup.analysis.lock().unwrap(); + let device = analysis.get_device_analysis(&setup.main_canon_path) + .expect("device analysis should exist"); + let duplicates: Vec<_> = device.errors.values().flatten() + .filter(|error|error.description.starts_with( + "Duplicate template name")).collect(); + assert_eq!(duplicates.len(), 1, "{:#?}", duplicates); + assert!(duplicates[0].span.path() + .ends_with("duplicate_template_import.dml")); + use dls::analysis::templating::objects::DMLNamedMember; + let bank = device.objects.values().find(|object| + object.identity() == "b").expect("bank b should exist"); + assert!(bank.get_object("g1").is_some()); + assert!(bank.get_object("g2").is_none()); + } + } + #[test] fn test_can_load_and_parse_dml_file() { init_logging(); @@ -869,7 +1038,7 @@ mod tests { &setup.ctx, &position, &mut HashSet::new()).unwrap(); assert_eq!(references.len(), 1); } - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_annotation_tests(&setup.ctx, &setup.main_file, &setup.annotations); } #[test] @@ -886,11 +1055,8 @@ mod tests { |error|error.description.as_str()).collect(); descriptions.sort(); assert_eq!(descriptions, vec![ - "Conflicting assignments to parameter 'shared_default'", "Conflicting assignments to parameter 'shared_default'", "Conflicting assignments to parameter 'shared_fixed'", - "Conflicting assignments to parameter 'shared_fixed'", - "Invalid assignment to auto-parameter 'shared_auto'", "Invalid assignment to auto-parameter 'shared_auto'", ], "unexpected parameter conflicts: {:#?}", errors); for error in errors { @@ -935,6 +1101,139 @@ mod tests { errors); } + #[test] + fn test_type_structs() { + init_logging(); + let setup = setup_test(&["type_structs.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_method_calls() { + init_logging(); + let setup = setup_test(&["type_method_calls.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_variables() { + init_logging(); + let setup = setup_test(&["type_variables.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_member_access() { + init_logging(); + let setup = setup_test(&["type_member_access.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_definitions() { + init_logging(); + let setup = setup_test(&["type_definitions.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_lookup_misc() { + init_logging(); + let setup = setup_test(&["lookup_misc.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_lookup_scopes() { + init_logging(); + let setup = setup_test(&["lookup_scopes.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_diag_structure() { + init_logging(); + let setup = setup_test(&["diag_structure.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_diag_parameters() { + init_logging(); + let setup = setup_test(&["diag_parameters.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_diag_methods() { + init_logging(); + let setup = setup_test(&["diag_methods.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_diag_types() { + init_logging(); + let setup = setup_test(&["diag_types.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_diag_objects() { + init_logging(); + let setup = setup_test(&["diag_objects.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_overrides() { + init_logging(); + let setup = setup_test(&["type_overrides.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_default_calls() { + init_logging(); + let setup = setup_test(&["type_default_calls.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_returns() { + init_logging(); + let setup = setup_test(&["type_returns.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_switch_scope() { + init_logging(); + let setup = setup_test(&["type_switch_scope.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_narrowing() { + init_logging(); + let setup = setup_test(&["type_narrowing.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_assignments() { + init_logging(); + let setup = setup_test(&["type_assignments.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_arrays() { + init_logging(); + let setup = setup_test(&["type_arrays.dml"]); + run_fixture_annotations(&setup); + } + #[test] fn test_toplevel_structure_parsed() { init_logging(); @@ -982,10 +1281,11 @@ mod tests { "}\n", ); - let (locations, operations) = parse_annotations(content, None); + let (locations, operations, diagnostics) = parse_annotations(content, None); assert_eq!(locations.len(), 2, "expected 2 @loc declarations"); assert_eq!(operations.len(), 2, "expected 2 operation annotations"); + assert!(diagnostics.is_empty()); assert!(locations.iter().any(|l| l.name == "t_def"), "missing t_def"); assert!(locations.iter().any(|l| l.name == "p_def"), "missing p_def"); @@ -1007,7 +1307,7 @@ mod tests { "code_here(); // @loc[1]=on_code\n", ); - let (locations, _) = parse_annotations(content, None); + let (locations, _, _) = parse_annotations(content, None); let upper = locations.iter().find(|l| l.name == "upper") .expect("missing upper"); @@ -1027,6 +1327,50 @@ mod tests { assert_eq!(on_code.col(), 0); } + #[test] + fn test_parse_diagnostic_annotations() { + init_logging(); + let content = concat!( + "method m(int argument) { // @loc[14]=argument_decl @loc[8]=method_decl\n", + "// @diag-error: first failure | related->argument_decl: declared here, related->method_decl: method here\n", + "// @diag-warning: second failure\n", + "call(); // @goto-def[1]->argument_decl\n", + "}\n", + ); + + let (locations, operations, diagnostics) = parse_annotations(content, None); + assert_eq!(locations.len(), 2); + assert_eq!(operations.len(), 1); + assert_eq!(diagnostics.len(), 2); + assert_eq!(diagnostics[0].location.line(), 3); + assert_eq!(diagnostics[0].severity, DiagnosticSeverity::ERROR); + assert_eq!(diagnostics[0].description, "first failure"); + assert_eq!(diagnostics[0].related.len(), 2); + assert_eq!(diagnostics[0].related[0].target.name, "argument_decl"); + assert_eq!(diagnostics[0].related[0].description, "declared here"); + assert_eq!(diagnostics[0].related[1].target.name, "method_decl"); + assert_eq!(diagnostics[0].related[1].description, "method here"); + assert_eq!(diagnostics[1].location.line(), 3); + assert_eq!(diagnostics[1].severity, DiagnosticSeverity::WARNING); + assert_eq!(diagnostics[1].description, "second failure"); + assert!(diagnostics[1].related.is_empty()); + } + + #[test] + fn test_parse_cross_file_diagnostic_related_target() { + init_logging(); + let content = concat!( + "// @diag-error: bad argument | related->imported_file.dml:argument_decl: declared here\n", + "call();\n", + ); + + let (_, _, diagnostics) = parse_annotations(content, None); + let related = &diagnostics[0].related[0]; + assert_eq!(related.target.filename.as_deref(), Some("imported_file.dml")); + assert_eq!(related.target.name, "argument_decl"); + assert_eq!(related.description, "declared here"); + } + #[test] fn test_negated_target_parsing() { init_logging(); @@ -1040,7 +1384,7 @@ mod tests { "}\n", ); - let (locations, operations) = parse_annotations(content, None); + let (locations, operations, _) = parse_annotations(content, None); assert_eq!(locations.len(), 3); assert_eq!(operations.len(), 1); @@ -1054,17 +1398,58 @@ mod tests { assert_eq!(op.targets[2].name, "quirky_def"); } + #[test] + fn test_type_lookup_limitations() { + init_logging(); + fn limitations_at(setup: &TestSetup, needle: &str, offset: usize, + lookup: LookupFn) -> Vec { + let content = std::fs::read_to_string(&setup.main_file) + .expect("failed to read fixture"); + let (row, line) = content.lines().enumerate() + .find(|(_, line)|line.contains(needle)) + .expect("fixture should contain needle"); + let col = line.find(needle).unwrap() + offset; + let position = make_file_position( + &setup.main_file, row as u32, col as u32); + let mut limitations = HashSet::new(); + lookup(&setup.ctx, &position, &mut limitations) + .expect("lookup failed"); + limitations.into_iter().map(|limitation|limitation.issue_num) + .collect() + } + + let variables = setup_test(&["type_variables.dml"]); + assert_eq!(limitations_at(&variables, "typeof(valid) bad_typeof_local", 14, + type_definitions_at_fp), vec![65], + "typeof-declared local should report a limitation"); + assert!(limitations_at(&variables, "copied = valid", 0, + type_definitions_at_fp).is_empty(), + "typed local should not report a limitation"); + assert!(limitations_at(&variables, "local int_alias copied", 6, + definitions_at_fp).is_empty(), + "typedef reference should not report a limitation"); + + let members = setup_test(&["type_member_access.dml"]); + assert_eq!(limitations_at(&members, "point_ptr()->x", 13, + definitions_at_fp), vec![65], + "member of a call result should report a limitation"); + assert!(limitations_at(&members, "uint32 x;", 0, + definitions_at_fp).is_empty(), + "builtin type reference should not report a limitation"); + } + #[test] fn test_token_at_position_recovers_full_identifier() { init_logging(); - // basic_lookup.dml line 10 (1-indexed) is: - // `template base_template { ...` - // so `base_template` occupies 0-indexed columns 9..=21. + // basic_lookup.dml has a line `template base_template { ...`, so + // `base_template` occupies 0-indexed columns 9..=21. let setup = setup_test(&["basic_lookup.dml"]); let content = std::fs::read_to_string(&setup.main_file) .expect("failed to read fixture"); let file: TextFile = content.parse().expect("failed to parse fixture text"); - let row = 9; // 0-indexed line 10 + let row = content.lines() + .position(|line|line.starts_with("template base_template ")) + .expect("fixture should declare base_template") as u32; let at = |col: u32| token_at_position( &setup.analysis, @@ -1095,7 +1480,7 @@ mod tests { fn test_basic_lookup_annotations() { init_logging(); let setup = setup_test(&["basic_lookup.dml"]); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); } #[test] @@ -1105,7 +1490,7 @@ mod tests { assert!(setup.annotations.iter().all(|annotation| matches!(annotation.token.as_str(), "container" | "value" | "read")), "unexpected token text: {:?}", setup.annotations); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_annotation_tests(&setup.ctx, &setup.main_file, &setup.annotations); } #[test] @@ -1113,7 +1498,7 @@ mod tests { init_logging(); let setup = setup_test(&["multi_level.dml"]); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); } #[test] @@ -1122,7 +1507,7 @@ mod tests { // Load both files - imported file first, then main file with annotations let setup = setup_test(&["cross_file_main.dml", "imported_file.dml"]); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); } #[test] @@ -1130,7 +1515,15 @@ mod tests { init_logging(); let setup = setup_test(&["simple_symbolic.dml"]); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_name_lookup() { + init_logging(); + let setup = setup_test(&["type_name_lookup.dml"]); + + run_fixture_annotations(&setup); } #[test] @@ -1141,7 +1534,21 @@ mod tests { // - Templates: find all instantiation sites let setup = setup_test(&["goto_impl_test.dml"]); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_interfaces() { + init_logging(); + let setup = setup_test(&["type_interfaces.dml"]); + run_fixture_annotations(&setup); + } + + #[test] + fn test_type_template_types() { + init_logging(); + let setup = setup_test(&["type_template_types.dml"]); + run_fixture_annotations(&setup); } /// Helper function to run annotation tests. @@ -1149,19 +1556,21 @@ mod tests { fn run_annotation_tests( ctx: &InitActionContext, test_file: &Path, - annotations: Vec, + annotations: &[ResolvedAnnotation], ) { let mut goto_def = Vec::new(); let mut goto_decl = Vec::new(); let mut goto_impl = Vec::new(); let mut find_refs = Vec::new(); + let mut goto_type_def = Vec::new(); - for ann in &annotations { + for ann in annotations { match ann.operation_type { OperationType::GotoDefinition => goto_def.push(ann), OperationType::GotoDeclaration => goto_decl.push(ann), OperationType::GotoImplementation => goto_impl.push(ann), OperationType::FindReferences => find_refs.push(ann), + OperationType::GotoTypeDefinition => goto_type_def.push(ann), } } @@ -1188,6 +1597,7 @@ mod tests { run(&goto_decl, OperationType::GotoDeclaration, declarations_at_fp); run(&goto_impl, OperationType::GotoImplementation, implementations_at_fp); run(&find_refs, OperationType::FindReferences, references_at_fp); + run(&goto_type_def, OperationType::GotoTypeDefinition, type_definitions_at_fp); assert!(all_sections.is_empty(), "{} annotation failure(s):\n\n{}", @@ -1195,6 +1605,132 @@ mod tests { all_sections.join("\n\n")); } + #[track_caller] + fn run_fixture_annotations(setup: &TestSetup) { + run_annotation_tests(&setup.ctx, &setup.main_file, &setup.annotations); + if !setup.diagnostics.is_empty() { + run_diagnostic_annotations(setup); + } + } + + fn related_matches( + expected: &DiagnosticRelated, + span: &ZeroSpan, + description: &str, + ) -> bool { + rel_to_test_files(&span.path()) == expected.target.filename() + && span.range.contains_pos(expected.target.position) + && description.contains(&expected.description) + } + + fn diagnostic_matches( + expected: &ResolvedDiagnosticAnnotation, + actual: &dls::analysis::DMLError, + ) -> bool { + let expected_file = expected.location.file_path.as_deref() + .map(rel_to_test_files) + .unwrap_or_default(); + if rel_to_test_files(&actual.span.path()) != expected_file + || actual.span.range.row_start.0 != expected.location.line() + || !actual.description.contains(&expected.description) + || actual.severity.unwrap_or(DiagnosticSeverity::ERROR) != expected.severity + || actual.related.len() != expected.related.len() + { + return false; + } + + let mut matched = vec![false; actual.related.len()]; + expected.related.iter().all(|related| { + actual.related.iter().enumerate().find(|(index, (span, description))| + !matched[*index] && related_matches(related, span, description)) + .map(|(index, _)| matched[index] = true) + .is_some() + }) + } + + fn format_diagnostic( + file: &Path, + error: &dls::analysis::DMLError, + ) -> String { + let related = error.related.iter().map(|(span, description)|format!( + "{}:{}:{}: {}", + rel_to_test_files(&span.path()), + span.range.row_start.0 + 1, + span.range.col_start.0 + 1, + description, + )).collect::>(); + format!( + "{}:{}:{} [{:?}]: {}{}", + rel_to_test_files(file), + error.span.range.row_start.0 + 1, + error.span.range.col_start.0 + 1, + error.severity.unwrap_or(DiagnosticSeverity::ERROR), + error.description, + if related.is_empty() { String::new() } + else { format!("\n related: {}", related.join("\n ")) }, + ) + } + + #[track_caller] + fn run_diagnostic_annotations(setup: &TestSetup) { + let analysis = setup.analysis.lock().unwrap(); + let fixture_paths: HashSet<_> = setup.fixture_paths.iter() + .map(|path|rel_to_test_files(path)) + .collect(); + let device_errors = analysis.get_device_analysis(&setup.main_canon_path) + .into_iter() + .flat_map(|device|device.errors.iter()) + .flat_map(|(path, errors)|errors.iter() + .map(move |error|(path.clone(), error))); + let isolated_errors = analysis.isolated_analysis.values() + .map(|isolated|&isolated.stored) + .flat_map(|isolated|isolated.errors.iter() + .map(move |error|(isolated.path.to_path_buf(), error))); + let mut actual: Vec<_> = device_errors.chain(isolated_errors) + .filter(|(path, _)| fixture_paths.contains(&rel_to_test_files(path))) + .filter(|(_, error)|!error.description.starts_with("Could not find file;")) + .collect(); + actual.sort_by_key(|(path, error)|( + rel_to_test_files(path), + error.span.range.row_start.0, + error.span.range.col_start.0, + error.description.clone(), + )); + + let mut consumed = vec![false; actual.len()]; + let mut missing = Vec::new(); + for expected in &setup.diagnostics { + let matched = actual.iter().enumerate().find(|(index, (_, error))| + !consumed[*index] && diagnostic_matches(expected, error)); + if let Some((index, _)) = matched { + consumed[index] = true; + } else { + let file = expected.location.file_path.as_deref() + .map(rel_to_test_files) + .unwrap_or_else(||"".to_string()); + missing.push(format!( + "{}:{} [{:?}]: {} ({} related)", + file, + expected.location.line() + 1, + expected.severity, + expected.description, + expected.related.len(), + )); + } + } + + let unexpected: Vec<_> = actual.iter().enumerate() + .filter(|(index, _)|!consumed[*index]) + .map(|(_, (path, error))|format_diagnostic(path, error)) + .collect(); + assert!(missing.is_empty() && unexpected.is_empty(), + "diagnostic annotation mismatch:\n missing:\n {}\n unexpected:\n {}", + if missing.is_empty() { "".to_string() } + else { missing.join("\n ") }, + if unexpected.is_empty() { "".to_string() } + else { unexpected.join("\n ") }); + } + /// Path of `p` relative to `tests/test_files/`, or the full path /// string if it falls outside that directory. fn rel_to_test_files(p: &Path) -> String { @@ -1418,7 +1954,7 @@ mod tests { let setup = setup_test( &["imports/test1_device.dml", "imports/test1_common.dml"], ); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); } /// Goto-def on an import in a commoncode file should also resolve, so @@ -1433,7 +1969,7 @@ mod tests { "imports/test2_common_b.dml", ], ); - run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + run_fixture_annotations(&setup); } /// When the same commoncode file is imported by two devices that diff --git a/tests/parser_recovery_tests.rs b/tests/parser_recovery_tests.rs new file mode 100644 index 00000000..4a2b8a64 --- /dev/null +++ b/tests/parser_recovery_tests.rs @@ -0,0 +1,73 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +//! Regression tests for parser termination during malformed-delimiter recovery. + +#[path = "common/parser_runner.rs"] +mod parser_runner; + +use std::time::Duration; + +use parser_runner::{Outcome, run_with_timeout}; + +const DELIMITER_REGRESSION_TIMEOUT: Duration = Duration::from_secs(5); + +fn assert_delimiter_recovery_terminates(declaration: &str) { + let input = format!( + "dml 1.4;\ndevice delimiter_recovery_test;\n{declaration}\n\ + session int valid_after_recovery;\n"); + let (outcome, message, elapsed) = + run_with_timeout(&input, DELIMITER_REGRESSION_TIMEOUT); + assert_eq!(outcome, Outcome::Ok, + "parser did not terminate normally after {elapsed:?}: {message:?}"); +} + +#[test] +fn delimiter_recovery_struct_member_semicolon() { + assert_delimiter_recovery_terminates( + "typedef struct { int value } struct_missing_member_semicolon_t;"); +} + +#[test] +fn delimiter_recovery_layout_member_semicolon() { + assert_delimiter_recovery_terminates( + "typedef layout \"big-endian\" { int value } \ + layout_missing_member_semicolon_t;"); +} + +#[test] +fn delimiter_recovery_bitfield_at() { + assert_delimiter_recovery_terminates( + "typedef bitfields 8 { uint1 bit [0]; } bitfield_missing_at_t;"); +} + +#[test] +fn delimiter_recovery_bitfield_brackets() { + assert_delimiter_recovery_terminates( + "typedef bitfields 8 { uint1 bit @ 0; } bitfield_missing_bracket_t;"); +} + +#[test] +fn delimiter_recovery_bitfield_range_operand() { + assert_delimiter_recovery_terminates( + "typedef bitfields 8 { uint1 bit @ [7:]; } \ + bitfield_missing_operand_t;"); +} + +#[test] +fn delimiter_recovery_bitfield_semicolon() { + assert_delimiter_recovery_terminates( + "typedef bitfields 8 { uint1 bit @ [0] } \ + bitfield_missing_semicolon_t;"); +} + +#[test] +fn delimiter_recovery_typeof_parenthesis() { + assert_delimiter_recovery_terminates( + "session typeof(value missing_typeof_rparen;"); +} + +#[test] +fn delimiter_recovery_array_declarator() { + assert_delimiter_recovery_terminates("session int malformed_array[;"); +} diff --git a/tests/test_files/basic_lookup.dml b/tests/test_files/basic_lookup.dml index fcade129..f88db6c7 100644 --- a/tests/test_files/basic_lookup.dml +++ b/tests/test_files/basic_lookup.dml @@ -4,6 +4,9 @@ dml 1.4; device test_device; +typedef int int_alias; // @loc[13]=int_alias_def +typedef uint64 uint64_alias; // @loc[16]=uint64_alias_def + // Rudimentary testing coverage // @loc[10]=base_template_def @@ -43,9 +46,10 @@ bank test_bank { // @goto-ref[6]->!test_register_parent_ref, test_bank_ref // @loc[16]=test_register_read method read() -> (uint64) { // @goto-ref[16]->test_register_read_ref - // @loc[26]=value_decl - // @loc[34]=use_reg_shadowed_param_1 - local uint64 value = shadowed_param; // @goto-ref[26]->value_ref_1,value_ref_2,value_ref_3 @goto-def-decl[34]->reg_shadowed_param_def + // @loc[32]=value_decl + // @loc[40]=use_reg_shadowed_param_1 + // @goto-def-decl[32]->value_decl @goto-type-def[32]->uint64_alias_def + local uint64_alias value = shadowed_param; // @goto-ref[32]->value_ref_1,value_ref_2,value_ref_3 @goto-def-decl[40]->reg_shadowed_param_def // @loc[13]=value_ref_1 // @loc[21]=test_register_this_ref // @loc[26]=use_reg_shadowed_param_2 @@ -59,8 +63,9 @@ bank test_bank { // @goto-ref[6]->!test_register_parent_ref, test_bank_ref } } - // @loc[21]=bank_m_value_def - method m(uint64 value) -> (uint64) { // @goto-ref[21]->bank_m_value_ref + // @loc[27]=bank_m_value_def + // @goto-def-decl[27]->bank_m_value_def @goto-type-def[27]->uint64_alias_def + method m(uint64_alias value) -> (uint64) { // @goto-ref[27]->bank_m_value_ref // @loc[16]=bank_m_value_ref // @loc[24]=use_bank_shadowed_param_2 return value + shadowed_param; // @goto-def-decl[16]->bank_m_value_def @goto-def-decl[24]->bank_shadowed_param_def @@ -79,8 +84,9 @@ bank test_bank { // @goto-ref[6]->!test_register_parent_ref, test_bank_ref // Method for testing method references and calls // @loc[8]=device_method_def method device_method() { // @goto-ref[8]->device_method_ref1,device_method_ref2 - // @loc[15]=x_decl - local int x = 10; + // @loc[21]=x_decl + // @goto-def-decl[21]->x_decl @goto-type-def[21]->int_alias_def + local int_alias x = 10; x = x + 1; // @goto-def-decl[5]->x_decl @goto-def-decl[9]->x_decl } @@ -95,11 +101,13 @@ group test_group { // @goto-ref[7]->test_group_ref } // Session and saved variables for testing -// @loc[13]=session_var_def -session int session_var; // @goto-ref[13]->session_var_ref1,session_var_ref2 +// @loc[19]=session_var_def +// @goto-def-decl[19]->session_var_def @goto-type-def[19]->int_alias_def +session int_alias session_var; // @goto-ref[19]->session_var_ref1,session_var_ref2 -// @loc[11]=saved_var_def -saved int saved_var; // @goto-ref[11]->saved_var_ref +// @loc[17]=saved_var_def +// @goto-def-decl[17]->saved_var_def @goto-type-def[17]->int_alias_def +saved int_alias saved_var; // @goto-ref[17]->saved_var_ref // Hooks for testing hook references // @loc[15]=test_hook_def @@ -142,16 +150,19 @@ method phantom_calls() { // @goto-ref[8]-> } // extern methods and values +extern typedef struct {} attr_value_t; // @loc[26]=attr_value_t_def // @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[34]=extern_foo_call + // @goto-def-decl[34]->extern_foo_decl + // @loc[21]=foo_called_decl @goto-def-decl[21]->foo_called_decl @goto-type-def[21]->int_alias_def + local int_alias foo_called = foo(false); // @loc[36]=extern_bar_ref // @goto-def-decl[36]->extern_bar_decl + // @loc[25]=bar_ref_decl @goto-def-decl[25]->bar_ref_decl @goto-type-def[25]->attr_value_t_def local attr_value_t* bar_ref = &bar; } diff --git a/tests/test_files/conditional_conflicts.dml b/tests/test_files/conditional_conflicts.dml index 881da359..13363d5a 100644 --- a/tests/test_files/conditional_conflicts.dml +++ b/tests/test_files/conditional_conflicts.dml @@ -4,6 +4,8 @@ dml 1.4; device conditional_conflicts; +typedef int int_alias; // @loc[13]=int_alias_def + template exclusive_method { method exclusive_name() {} } @@ -102,5 +104,6 @@ template exclusive_auto_assignment { extern int extern_int; #if (false) { - session int extern_int; + // @loc[23]=extern_int_decl @goto-def-decl[23]->extern_int_decl @goto-type-def[23]->int_alias_def + session int_alias extern_int; } diff --git a/tests/test_files/cross_file_main.dml b/tests/test_files/cross_file_main.dml index 176fbb32..05574a69 100644 --- a/tests/test_files/cross_file_main.dml +++ b/tests/test_files/cross_file_main.dml @@ -4,6 +4,8 @@ dml 1.4; device cross_file_test; +typedef int int_alias; // @loc[13]=int_alias_def + import "imported_file.dml"; // Bank using imported template @@ -16,8 +18,9 @@ bank imported_bank is imported_template { // @goto-def-decl[27]->imported_file.d // Reference to imported method this.imported_method(); // @goto-def-decl[14]->imported_file.dml:imported_method // Reference to imported param - // @loc[28]=imported_param_ref - local int x = this.imported_param; // @goto-def-decl[28]->imported_file.dml:imported_param + // @loc[34]=imported_param_ref + // @loc[25]=x_decl @goto-def-decl[25]->x_decl @goto-type-def[25]->int_alias_def + local int_alias x = this.imported_param; // @goto-def-decl[34]->imported_file.dml:imported_param } } diff --git a/tests/test_files/diag_methods.dml b/tests/test_files/diag_methods.dml new file mode 100644 index 00000000..a5a1cd6d --- /dev/null +++ b/tests/test_files/diag_methods.dml @@ -0,0 +1,149 @@ +dml 1.4; +device diag_methods; + +// Method override and inheritance errors, mirroring DMLC's EMETH, EDMETH, +// EAMBINH and EABSTEMPLATE + +typedef uint32 int_alias_t; + +template t { + inline method inline_args(inline x, int y) default {} // @loc[38]=inline_x @loc[45]=typed_y + method throwing() throws default {} // @loc[12]=throwing_decl + method int_args(uint32 a, int_alias_t b) default {} // @loc[28]=int_arg_a + method int_return() -> (int) default { return 0; } // @loc[12]=int_return_decl @loc[29]=int_return_type + method int_return_2() -> (int_alias_t, uint32) default { return (0, 0); } // @loc[44]=second_return_type + independent method indep() default {} // @loc[24]=indep_decl + method with_abstract_decls(); // @loc[12]=abstract_decl + // @diag-error: Wrong number of arguments in method override | related->abstract_decl: Overridden method declared here + method with_abstract_decls(int i) default {} + shared method sm() {} // @loc[19]=shared_sm + method nondefault() {} // @loc[12]=nondefault_decl +} + +is t; + +// @diag-error: Cannot override non-default method | related->shared_sm: Non-default method declared here +method sm() {} + +// @diag-error: Mismatching argument type in method override | related->inline_x: Corresponding argument declared here +// @diag-error: Mismatching argument type in method override | related->typed_y: Corresponding argument declared here +inline method inline_args(int x, inline y) {} + +// @diag-error: Non-throwing method cannot override throwing method | related->throwing_decl: Throwing method declared here +method throwing() {} + +// @diag-error: Mismatching argument type in method override | related->int_arg_a: Corresponding argument declared here +method int_args(char a, int_alias_t b) default {} + +// @diag-error: Mismatching return type in method override | related->int_return_type: Corresponding return type declared here +method int_return() -> (char) default { return 0; } + +// @diag-error: Mismatching return type in method override | related->second_return_type: Corresponding return type declared here +method int_return_2() -> (int_alias_t, char) default { return (0, 0); } + +// @diag-error: Cannot override non-default method | related->nondefault_decl: Non-default method declared here +method nondefault() {} + +group g4 is t { + // @diag-error: qualified as independent, but the other is not | related->indep_decl: Overridden method declared here + method indep() {} + // @diag-error: qualified as independent, but the other is not | related->int_return_decl: Overridden method declared here + independent method int_return() -> (int) { return 0; } +} + +// Typedefs are transparent in override signatures +port p is t { + method int_args(int_alias_t a, uint32 b) {} + method int_return_2() -> (uint32, int_alias_t) { return (0, 0); } +} + +template asm1 { + shared method m(); // @loc[19]=asm1_m +} + +template asm2 { + shared method m(); // @loc[19]=asm2_m +} + +// At most one abstract shared declaration is allowed, even when overridden +// @diag-error: Conflicting definitions of method 'm' in asm4 | related->asm1_m: in asm1, related->asm2_m: in asm2 +template asm4 is (asm1, asm2) { + shared method m() {} +} + +template asm6 is asm1 { + shared method m() default {} // @loc[19]=asm6_m +} + +template asm7 is asm1 { + shared method m() default {} // @loc[19]=asm7_m +} + +// No error: the template's own definition overrides both parents +template asm8 is (asm6, asm7) { + shared method m() {} +} + +template asm9 is asm1 { + shared method m() {} // @loc[19]=asm9_m +} + +// @diag-error: Conflicting definitions of method 'm' in asm10 | related->asm6_m: in asm6, related->asm9_m: in asm9 +template asm10 is (asm6, asm9) {} + +// @diag-error: Conflicting definitions of method 'm' in ambiguous | related->asm6_m: Conflicting definition here, related->asm7_m: Conflicting definition here +group ambiguous is (asm6, asm7); + +group resolved is (asm6, asm7) { + method m() {} +} + +// Redundant instantiations do not hide which definition wins +group redundant is (asm6, asm7, asm1, asm8); + +template shared_base { + shared method shared_default() default {} +} + +// No error: a non-shared method may override a shared one +template nonshared_override is shared_base { + method shared_default() {} +} + +template nonshared_base { + method nonshared_default() default {} // @loc[12]=nonshared_decl +} + +template shared_override is nonshared_base { + // @diag-error: Shared method cannot override non-shared method | related->nonshared_decl: Overridden method declared here + shared method nonshared_default() {} +} + +template abstract_base { // @loc[10]=abstract_base_template + method abs(); +} + +// No error: non-shared abstract declarations may be repeated +template abstract_again is abstract_base { + method abs(); +} + +group implements_abstract is abstract_again { + method abs() {} +} + +template throws_base { + method may_throw() throws default {} // @loc[12]=may_throw_decl + method no_throw() default {} // @loc[12]=no_throw_decl +} + +group throws_mismatch is throws_base { + // @diag-error: Non-throwing method cannot override throwing method | related->may_throw_decl: Throwing method declared here + method may_throw() {} + // @diag-error: Throwing method cannot override non-throwing method | related->no_throw_decl: Non-throwing method declared here + method no_throw() throws {} +} + +// @diag-error: requires method 'abs' to be defined | related->abstract_base_template: Template defined here +group unimplemented is abstract_base { +} diff --git a/tests/test_files/diag_objects.dml b/tests/test_files/diag_objects.dml new file mode 100644 index 00000000..555dddd9 --- /dev/null +++ b/tests/test_files/diag_objects.dml @@ -0,0 +1,336 @@ +dml 1.4; // @loc[1]=device_start +device diag_objects; + +// Object-level declaration errors, mirroring DMLC's ENAMECOLL, EAINCOMP and +// EERRSTMT tests + +// @diag-error: Name collision in declaration on 'c0' | related->c0_connect: also declared here +param c0 = 5; +connect c0; // @loc[9]=c0_connect + +// @diag-error: Name collision in declaration on 'c_hook' | related->c_hook_group: also declared here +hook() c_hook; +group c_hook; // @loc[7]=c_hook_group + +// A hook cannot be declared twice, not even with the same type +// @diag-error: Name collision in declaration on 'c_hook2' | related->c_hook2_second: also declared here +hook(int) c_hook2; +hook(int) c_hook2; // @loc[11]=c_hook2_second + +// @diag-error: Name collision in declaration on 'c1' | related->c1_method: also declared here +param c1 = 5; +method c1() {} // @loc[8]=c1_method + +// @diag-error: Name collision in declaration on 'c2' | related->c2_session: also declared here +param c2 = 1; +session int c2; // @loc[13]=c2_session + +// @diag-error: Inconsistent object type for c3 | related->c3_group: mismatching type here +connect c3; +group c3; // @loc[7]=c3_group + +// @diag-error: Name collision in declaration on 'c4' | related->c4_method: also declared here +connect c4; +method c4() {} // @loc[8]=c4_method + +// @diag-error: Name collision on 'c5' | related->c5_session: Previously defined here +method c5() {} +session int c5; // @loc[13]=c5_session + +// @diag-error: Name collision in declaration on 'c6' | related->c6_session: also declared here +connect c6; +session int c6; // @loc[13]=c6_session + +session int c8; // @loc[13]=c8_first +// @diag-error: Name collision on 'c8' | related->c8_first: Previously defined here +session int c8; + +method bar() default {} // @loc[8]=bar_first +// @diag-error: Name collision on 'bar' | related->bar_first: Previously defined here +method bar() default {} + +// @diag-error: Name collision in declaration on 'x' | related->x_bank: also declared here +extern int x; +bank x; // @loc[6]=x_bank + +bank b { + // @diag-error: Inconsistent object type for r | related->r_group: mismatching type here + register r[i < 4] size 1 @ i; + group r[i < 4]; // @loc[11]=r_group +} + +// Repeated declarations of the same kind merge +group same_kind_twice { +} +group same_kind_twice { +} + +// @diag-error: Inconsistent object type for kind_change | related->kind_change_group: mismatching type here +bank kind_change { +} +group kind_change { // @loc[7]=kind_change_group +} + +// @diag-error: Mismatching number of dimensions in object declaration | related->dims_change_array: mismatching dimensionality here +group dims_change { +} +group dims_change[i < 2] { // @loc[18]=dims_change_array +} + +// @diag-error: Name collision on index variable 'i' | related->ga_first_index: Previously defined here +group ga[i < 4][i < 4]; // @loc[10]=ga_first_index + +// Discarded index variables neither collide nor mismatch named ones +group discard_dims[_ < 2][_ < 3]; +group discard_named[_ < 2] { + param p = j; // @goto-def[15]->discard_named_j +} +group discard_named[j < 2]; // @loc[21]=discard_named_j + +template clashing_shared_methods { + shared method m() default {} // @loc[19]=shared_m_first + // @diag-error: Name collision on 'm' | related->shared_m_first: Previously defined here + shared method m() default {} + param i : int; // @loc[11]=typed_i_first + // @diag-error: Name collision on 'i' | related->typed_i_first: Previously defined here + param i : int; + // @diag-error: Name collision on 'mp' | related->mp_param: Previously defined here + shared method mp() default {} + param mp : int; // @loc[11]=mp_param + session int ses; // @loc[17]=ses_first + // @diag-error: Name collision on 'ses' | related->ses_first: Previously defined here + session int ses; + param pm : int; // @loc[11]=pm_param + // @diag-error: Name collision on 'pm' | related->pm_param: Previously defined here + shared method pm() default {} +} + +// A method cannot override a parameter, and a parameter cannot override +// anything +template abstract_param { + param p0 : int; // @loc[11]=abstract_p0 +} + +template shared_method_override is abstract_param { + // @diag-error: Name collision on 'p0' | related->abstract_p0: Previously defined here + shared method p0(); +} + +template method_override is abstract_param { + // @diag-error: Name collision in declaration on 'p0' | related->abstract_p0: also declared here, related->po_p0: also declared here + method p0() {} +} + +template typed_param_override is abstract_param { + // @diag-error: Name collision on 'p0' | related->abstract_p0: Previously defined here + param p0 : int; +} + +group po is method_override { + param p0 = 1; // @loc[11]=po_p0 +} + +template abstract_method { + // @diag-error: Name collision in declaration on 'am' | related->untyped_am: also declared here + shared method am(); // @loc[19]=abstract_am + // @diag-error: Name collision in declaration on 'dm' | related->untyped_dm: also declared here + shared method dm() default {} // @loc[19]=default_dm + shared method dm2() default {} // @loc[19]=default_dm2 + session int ses2; // @loc[17]=abstract_ses2 +} + +template param_override is abstract_method { + // @diag-error: Name collision on 'am' | related->abstract_am: Previously defined here + param am : int; + // @diag-error: Name collision on 'dm' | related->default_dm: Previously defined here + param dm : int; + // @diag-error: Name collision on 'dm2' | related->default_dm2: Previously defined here + session int dm2; + // @diag-error: Name collision on 'ses2' | related->abstract_ses2: Previously defined here + session int ses2; +} + +template param_untyped_override is abstract_method { + param am = 5; // @loc[11]=untyped_am + param dm = 6; // @loc[11]=untyped_dm +} + +group pu is param_untyped_override; + +template structural_clash { + // @diag-error: Conflicting assignments to parameter 'sp' | related->nested_sp: Conflicting assignment here + param sp default 1; + group sg { + param sp default 2; // @loc[9]=nested_sp + } +} +group sc is structural_clash { + group sg is structural_clash; +} + +template sm { + // @diag-error: Name collision in declaration on 'm' | related->tb_register_m: also declared here + shared method m() default {} +} + +bank tb is sm { + register m size 1 @ 0; // @loc[14]=tb_register_m +} + +template p_clash { + // Overriding both assignments does not hide the clash + // @diag-error: Conflicting assignments to parameter 'p' | related->p_clash_second: Conflicting assignment here + param p default 1; + param p default 2; // @loc[5]=p_clash_second +} +group p_clash is p_clash { param p = 3; } + +port p0 { + // @diag-error: Conflicting assignments to parameter 'foo' | related->foo_second: Conflicting assignment here + param foo = 1; + param foo = 1; // @loc[5]=foo_second +} + +port p2 { + // @diag-error: Conflicting assignments to parameter 'baz' | related->baz_second: Conflicting assignment here + param baz default 1; + param baz default 1; // @loc[5]=baz_second +} + +// The device object is logged as 'dev', so no top-level object may be +// @diag-error: Name collision on 'dev' | related->device_start: The device itself is logged as 'dev' +group dev { +} + +bank b2 { + register r1 size 1 @ 0 {} // @loc[14]=r1_register + register r2 size 1 @ 4 { + // @diag-error: Name collision on 'r1' | related->r1_register: Previously defined here + param name = "r1"; + } + register r3 size 1 @ 8 { + param name = "r_collision"; // @loc[22]=r_collision_first + } + register r4 size 1 @ 12 { + // @diag-error: Name collision on 'r_collision' | related->r_collision_first: Previously defined here + param name = "r_collision"; + } +} + +template c7 { } // @loc[10]=c7_first +// @diag-error: Duplicate template name; 'c7' | related->c7_first: Previously defined here +template c7 { } + +template c9 {} // @loc[10]=c9_template +// @diag-error: Typedef name 'c9' collides with a template name | related->c9_template: Template defined here +typedef int c9; + +// An extern typedef and an extern of the same name do not collide +extern typedef int t0; +extern int t0; + +typedef int t1; // @loc[13]=t1_first +// @diag-error: Duplicate typedef name; 't1' | related->t1_first: Previously defined here +typedef int t1; + +loggroup lg; // @loc[10]=lg_first @goto-def-decl[10]->lg_first +// @diag-error: Duplicate loggroup name; 'lg' | related->lg_first: Previously defined here +// @loc[10]=lg_second @goto-def-decl[10]->lg_second,!lg_first +loggroup lg; + +// Loggroups in mutually exclusive top-level '#if' branches do not collide +#if (true) { + loggroup lg_cond; // @loc[14]=lg_cond_true +} #else { + loggroup lg_cond; // @loc[14]=lg_cond_false +} +#if (true) { + // @diag-error: Duplicate loggroup name; 'lg' | related->lg_first: Previously defined here + loggroup lg; +} + +method log_cond() { + log info, 1, lg_cond: "x"; // @goto-def[18]->lg_cond_true,lg_cond_false +} + +method m1() { } +method m2() { } + +export m1 as "m_dup"; // @loc[14]=m_dup_first +// @diag-error: Duplicate export name; 'm_dup' | related->m_dup_first: Previously defined here +export m2 as "m_dup"; + +// Plain 'if' does not exclude an error statement, only '#if' does +method errors() { + if (false) { + // @diag-error: forced compilation error in source code + error; + } + if (true) { + } else { + // @diag-error: forced compilation error in source code + error; + } +} + +// '#if' branches are only checked when the condition is known +param unknown_condition default true; +method hashif_errors() { + #if (true) { + // @diag-error: taken branch + error "taken branch"; + } #else { + error "untaken else"; + } + #if (!dml_1_4) { + error "untaken if"; + } #else { + // @diag-error: taken else + error "taken else"; + } + #if (unknown_condition) { + error "unknown condition"; + } + switch (1) { + case 1: + #if (true && !false) { + // @diag-error: taken switch branch + error "taken switch branch"; + } #else { + error "untaken switch branch"; + } + } +} + +// Overridden bodies are dead unless reached through 'default()' +template must_override { + method dead() default { + error "Must be overridden"; + } + method reached() default { + // @diag-error: reached through default + error "reached through default"; + } + method dead_behind_reached() default { + error "behind an override that is itself dead"; + } +} +template middle is must_override { + method dead_behind_reached() default { + default(); + } +} +group overrides is middle { + method dead() {} + method reached() { + default(); + } + method dead_behind_reached() {} +} + +// @diag-error: unconditional error object +error "unconditional error object"; + +#if (false) { + error "guarded"; +} diff --git a/tests/test_files/diag_parameters.dml b/tests/test_files/diag_parameters.dml new file mode 100644 index 00000000..821c99c0 --- /dev/null +++ b/tests/test_files/diag_parameters.dml @@ -0,0 +1,102 @@ +dml 1.4; +device diag_parameters; + +// Parameter resolution errors, mirroring DMLC's ENPARAM, EINVOVER, EAMBINH +// and the parameter cases of ENAMECOLL + +// @diag-error: No assignment to parameter 'x' | related->untyped_decl: Declared here +group missing_value { + param x; // @loc[5]=untyped_decl +} + +template typed_param { + param p : int; // @loc[5]=typed_decl +} + +// @diag-error: No assignment to parameter 'p' | related->typed_decl: Declared here +group missing_typed is typed_param { +} + +group typed_assigned is typed_param { + param p = 4; +} + +template fixed_value { + param fixed = 5; // @loc[5]=fixed_def +} + +group overrides_fixed { + is fixed_value; + // @diag-error: Definition overrides non-default parameter | related->fixed_def: Non-default declaration here + param fixed default 3; +} + +group double_assignment { + // @diag-error: Conflicting assignments to parameter 'foo' | related->second_foo: Conflicting assignment here + param foo = 1; + param foo = 1; // @loc[5]=second_foo +} + +group double_default { + // @diag-error: Conflicting assignments to parameter 'baz' | related->second_baz: Conflicting assignment here + param baz default 1; + param baz default 2; // @loc[5]=second_baz +} + +template defaulted { + param d default 1; +} + +group overrides_default is defaulted { + param d = 2; +} + +template tp1 { + param tp : int; // @loc[11]=tp1_decl +} + +template tp2 { + param tp : int; // @loc[11]=tp2_decl +} + +// @diag-error: Conflicting definitions of 'tp' in tp3 | related->tp1_decl: in tp1, related->tp2_decl: in tp2 +template tp3 is (tp1, tp2) { + param tp = 3; +} + +group uses_tp3 is tp3 { +} + +template structural_clash { + // @diag-error: Conflicting assignments to parameter 'sp' | related->inner_sp: Conflicting assignment here + param sp default 1; + group sg { + param sp default 2; // @loc[9]=inner_sp + } +} + +group clash_outer is structural_clash { + group sg is structural_clash; +} + +// An 'in each' declaration overrides the extended template, but not the +// surrounding template, so these two defaults are ambiguous +template sub { +} + +template in_each_default { + in each sub { + // @diag-error: Conflicting assignments to parameter 'ie' | related->port_ie: Conflicting assignment here + param ie default 1; + } +} + +template port_default { + port pd { + is sub; + param ie default 2; // @loc[9]=port_ie + } +} + +is in_each_default; +is port_default; diff --git a/tests/test_files/diag_structure.dml b/tests/test_files/diag_structure.dml new file mode 100644 index 00000000..0aaddf5c --- /dev/null +++ b/tests/test_files/diag_structure.dml @@ -0,0 +1,121 @@ +dml 1.4; +device diag_structure; + +// Errors found in a single file, independent of any device + +// @diag-error: bitorder must be 'le' or 'be' +bitorder mixed; + +// @diag-error: Version declaration must be first statement in file +dml 1.4; +// @diag-error: Device declaration must be second statement in file +device again; +// @diag-error: Bitorder declaration must follow a device declaration +bitorder be; +// @diag-error: Provisional declaration must immediately follow the version declaration +provisional explicit_param_decls; + +group g { + // @diag-error: Constant declaration not allowed in an object declaration + constant c = 1; + // @diag-error: Extern declaration not allowed in an object declaration + extern int e; + // @diag-error: Template declaration not allowed in an object declaration + template t {} + // @diag-error: Header declaration not allowed in an object declaration + header %{ %} + // @diag-error: Footer declaration not allowed in an object declaration + footer %{ %} + // @diag-error: Loggroup declaration not allowed in an object declaration + loggroup lg; + #if (true) { + // @diag-error: Loggroup declaration not allowed in '#if' + loggroup lg_if; + } + // @diag-error: Typedef declaration not allowed in an object declaration + typedef int t2; +} + +#if (true) { + // @diag-error: Param declaration not allowed directly inside a #if block + param p = 1; +} + +// @diag-error: No assignment to parameter 'tp' | related->tp_decl: Declared here +group typed_param_holder { + // @diag-error: Typed parameter declaration only permitted in top level template block + param tp : int; // @loc[5]=tp_decl +} + +// @diag-error: Shared method declaration only permitted in top level template block +shared method sm() {} + +// @diag-error: inline arguments require method to be declared as inline +method mi(inline a) {} + +// @diag-error: only use inline if there are untyped arguments +inline method im(int a) {} + +// @diag-error: a method declared as startup must also be declared independent +startup method st() {} + +// @diag-error: a method declared as startup cannot have input parameters +independent startup method st_args(int a) {} + +independent startup memoized method st_mem() -> (int) { return 1; } + +// @diag-error: a method declared as memoized must also be declared independent +// @diag-error: a method declared as memoized must also be declared startup +memoized method mm() -> (int) { return 1; } + +// @diag-error: a method declared as memoized must also be declared startup +independent memoized method mm_void() {} + +// @diag-error: a method declared as memoized must either throw or have return types +independent startup memoized method mm_nothing() {} + +// @diag-error: a startup method not declared as memoized cannot throw +independent startup method st_throws() throws {} + +// @diag-error: a startup method not declared as memoized cannot have return values +independent startup method st_returns() -> (int) { return 1; } + +// @diag-error: a startup method cannot be declared as default +independent startup method st_default() default {} + +typedef layout "middle-endian" { // @diag-error: Must be 'big-endian' or 'little-endian' + uint8 a; +} bad_layout; + +typedef bitfields 0 { // @diag-error: Bitfields width must be an integer between 1 and 64 + uint8 b @ [7:0]; +} bad_bitfields; + +typedef struct { + // @diag-error: missing name in declaration + int; +} unnamed_member; + +method m1() {} +export m1 as "m1_export"; + +method body() { + // @diag-error: Too large integer constant + local int big = 99999999999999999999999999; + // @diag-error: Too large integer constant + local int small = -99999999999999999999999999; + // @diag-error: Wrong number of initializers in declaration + local (int w1, int w2) = (1, 2, 3); + // @diag-error: Invalid log kind + log foo: "x"; + // @diag-error: Can only compose string literals with '+', got '-' + log info: "a" - "b"; + // @diag-error: Expected string literal, got integer constant + log info: "a" + 1; + // @diag-error: Expected composed string literal + log info: big; + // @diag-error: Expected time unit ('s', 'cycles', 'ps') + after 1 xs: body(); + // @diag-error: Expected '*' before 'const' + local const uint8 *cast_ptr = cast(big, uint8 const *); +} diff --git a/tests/test_files/diag_types.dml b/tests/test_files/diag_types.dml new file mode 100644 index 00000000..fe2eb802 --- /dev/null +++ b/tests/test_files/diag_types.dml @@ -0,0 +1,129 @@ +dml 1.4; +device diag_types; + +// Type errors, mirroring DMLC's ETYPE, EANONSTRUCT and ENAMECOLL for types + +// @diag-error: Unknown type: 'undefined_ret_t' +extern undefined_ret_t undef_ret(); +// @diag-error: Unknown type: 'undefined_arg_t' +extern void undef_arg(undefined_arg_t); +// @diag-error: Unknown type: 'undefined_val_t' +extern undefined_val_t undef_val; +// @diag-error: Unknown type: 'undefined_func_t' +typedef undefined_func_t undef_func(); +// @diag-error: Unknown type: 'undefined_typedef_arg_t' +extern typedef void undef_typedef_arg(undefined_typedef_arg_t); +// @diag-error: Unknown type: 'undefined_typedef_val_t' +extern typedef undefined_typedef_val_t undef_typedef_val; + +typedef struct { + // @diag-error: Unknown type: 'undefined_member_t' + undefined_member_t member; + // @diag-error: Unknown type: 'undefined_pointee_t' + undefined_pointee_t *pointer; + // @diag-error: Unknown type: 'undefined_element_t' + undefined_element_t array[4]; +} has_undefined_members; + +template t { + // @diag-error: Unknown type: 'undefined_hook_t' + shared hook(undefined_hook_t) h; + // @diag-error: Unknown type: 'undefined_session_t' + session undefined_session_t ses; + // @diag-error: Unknown type: 'undefined_saved_t' + saved undefined_saved_t sav; + // @diag-error: Unknown type: 'undefined_param_t' + param p : undefined_param_t; + // @diag-error: Unknown type: 'undefined_marg_t' + // @diag-error: Unknown type: 'undefined_mret_t' + shared method m(bool a, undefined_marg_t b) -> (undefined_mret_t); + // @diag-error: Unknown type: 'undefined_narg_t' + // @diag-error: Unknown type: 'undefined_nret_t' + shared method n(bool a, undefined_narg_t b) -> (undefined_nret_t) { + assert false; + } +} + +// Templates are checked even when never instantiated, except for the bodies +// and signatures of non-shared methods +template never_instantiated { + // @diag-error: Unknown type: 'undefined_unused_session_t' + session undefined_unused_session_t s; + method nonshared(undefined_nonshared_t x) { + local undefined_nonshared_local_t y; + } +} + +// Declarations behind a false '#if' are not checked +#if (false) { + session undefined_hidden_t hidden; + method hidden_method(undefined_hidden_arg_t x) {} +} + +group uses_t is t { + param p = 0; + method m(bool a, int b) -> (int) { + return 0; + } +} + +method locals() { + // @diag-error: Unknown type: 'undefined_local_t' + local undefined_local_t l; + // @diag-error: Unknown type: 'undefined_const_t' + local const undefined_const_t *cp; +} + +// @diag-error: Cannot use anonymous struct type in argument type +// @diag-error: Cannot use anonymous struct type in return type +method anonymous_arg(struct { int i; } *arg) -> (struct { int j; }) { +} + +typedef struct { + int x; +} known_t; + +method fields(known_t k) -> (int) { + // @diag-error: Struct type has no field named 'y' + return k.y; +} + +typedef int dup_t; // @loc[13]=first_dup +// @diag-error: Duplicate typedef name; 'dup_t' | related->first_dup: Previously defined here +typedef int dup_t; + +template typedef_collision { // @loc[10]=colliding_template +} +// @diag-error: Typedef name 'typedef_collision' collides with a template name | related->colliding_template: Template defined here +typedef int typedef_collision; + +// Built-in type names, as accepted by DMLC's parse_type and initial typedefs +method builtins() { + local uint1 a; + local int64 b; + local uint64_be_t c; + local int24_le_t d; + local bool e; + local double f; + local float g; + local char h; + local long i; + local ulong j; + local size_t k; + local ssize_t l; + local int64_t m; + local uint64_t n; + local void *o; + local integer_t p; + local uinteger_t q; + // @diag-error: Unknown type: 'int0' + local int0 bad_width; + // @diag-error: Unknown type: 'int65' + local int65 too_wide; + // @diag-error: Unknown type: 'uint8_xe_t' + local uint8_xe_t bad_endian; + // @diag-error: Unknown type: 'short' + local short bad_short; + // @diag-error: Unknown type: 'unsigned' + local unsigned bad_unsigned; +} diff --git a/tests/test_files/duplicate_template_device.dml b/tests/test_files/duplicate_template_device.dml new file mode 100644 index 00000000..8c50ed46 --- /dev/null +++ b/tests/test_files/duplicate_template_device.dml @@ -0,0 +1,7 @@ +dml 1.4; +device duplicate_template_device; +import "duplicate_template_import.dml"; +template t { + group g1 { method m() { local int x = 1; x = 2; } } +} +bank b is t; diff --git a/tests/test_files/duplicate_template_import.dml b/tests/test_files/duplicate_template_import.dml new file mode 100644 index 00000000..ad6988b9 --- /dev/null +++ b/tests/test_files/duplicate_template_import.dml @@ -0,0 +1,4 @@ +dml 1.4; +template t { + group g2 { method m() { local int x = 1; x = 2; } } +} diff --git a/tests/test_files/goto_impl_test.dml b/tests/test_files/goto_impl_test.dml index 01ab14d5..aeef3464 100644 --- a/tests/test_files/goto_impl_test.dml +++ b/tests/test_files/goto_impl_test.dml @@ -27,7 +27,7 @@ bank container_bank { } // @loc[10]=multi_use_tmpl -template multi_use_template { // @goto-impl[10]->bank_multi_inst,group_multi_inst,reg_multi_inst @goto-ref[10]->bank_multi_inst,group_multi_inst,reg_multi_inst,!multi_use_template_as_type_1,!multi_use_template_as_type_2 +template multi_use_template { // @goto-impl[10]->bank_multi_inst,group_multi_inst,reg_multi_inst @goto-ref[10]->bank_multi_inst,group_multi_inst,reg_multi_inst,multi_use_template_as_type_1,multi_use_template_as_type_2 param common default 42; } @@ -45,11 +45,11 @@ bank container_for_register { } } -// Make sure goto-impl on template does not find template as type reference +// Make sure goto-impl on template does not find template type references method m() { - // TODO: Type references currently do not work // @loc[11]=multi_use_template_as_type_1 @loc[52]=multi_use_template_as_type_2 - local multi_use_template m = cast(group_multi, multi_use_template); // @goto-def-decl[11]->!multi_use_tmpl @goto-def-decl[52]->!multi_use_tmpl + // @loc[30]=m_decl @goto-def-decl[30]->m_decl @goto-type-def[30]->multi_use_tmpl + local multi_use_template m = cast(group_multi, multi_use_template); // @goto-def-decl[11]->multi_use_tmpl @goto-def-decl[52]->multi_use_tmpl } // Test 3: Multi-template in each - requires ALL templates diff --git a/tests/test_files/imported_file.dml b/tests/test_files/imported_file.dml index 3f538c96..edd1bc4e 100644 --- a/tests/test_files/imported_file.dml +++ b/tests/test_files/imported_file.dml @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 and MIT dml 1.4; +typedef int imported_int_alias; // @loc[13]=imported_int_alias_def + // This file defines types and templates to be imported by cross_file_main.dml // Template to be imported and used @@ -19,8 +21,9 @@ template imported_template { // @goto-impl[10]->cross_file_main.dml:imported_tem // Method that references a parameter defined in the importing file method use_bank_param() { // This will reference bank_specific_param from cross_file_main.dml - // @loc[28]=bank_specific_param_ref - local int y = this.bank_specific_param; // @goto-def[28]->cross_file_main.dml:bank_specific_param + // @loc[43]=bank_specific_param_ref + // @loc[34]=y_decl @goto-def-decl[34]->y_decl @goto-type-def[34]->imported_int_alias_def + local imported_int_alias y = this.bank_specific_param; // @goto-def[43]->cross_file_main.dml:bank_specific_param } } diff --git a/tests/test_files/lookup_misc.dml b/tests/test_files/lookup_misc.dml new file mode 100644 index 00000000..9f5a4c08 --- /dev/null +++ b/tests/test_files/lookup_misc.dml @@ -0,0 +1,74 @@ +dml 1.4; +device lookup_misc; + +typedef int int_alias; // @loc[13]=int_alias_def + +// Lookups of less common declarations and expression forms + +constant C = 4; // @loc[10]=constant_def +loggroup lg_a; // @loc[10]=loggroup_def @goto-ref[10]->lg_a_use + +typedef layout "big-endian" { + uint16 la; // @loc[12]=layout_field_def +} lay_t; // @loc[3]=lay_t_def + +typedef bitfields 8 { + uint4 bf @ [3:0]; // @loc[11]=bitfield_def +// @loc[3]=bf_t_def +} bf_t; + +// @loc[15]=s_lay_decl @goto-def-decl[15]->s_lay_decl @goto-type-def[15]->lay_t_def +session lay_t s_lay; +// @loc[14]=s_bf_decl @goto-def-decl[14]->s_bf_decl @goto-type-def[14]->bf_t_def +session bf_t s_bf; + +template tq { // @loc[10]=tq_def + method tq_m() default {} // @loc[12]=tq_m_def + param tq_p : int; // @loc[11]=tq_p_def + shared method tq_s() {} // @loc[19]=tq_s_def +} + +// @loc[12]=tq_ref_decl @goto-def-decl[12]->tq_ref_decl @goto-type-def[12]->tq_def +session tq tq_ref; + +hook() h; // @loc[8]=hook_def + +method target() {} // @loc[8]=target_def + +export target as "target_export"; // @goto-def-decl[8]->target_def + +group arr[i < C]; // @goto-def-decl[15]->constant_def + +group g is tq { + param tq_p = 1; // @loc[11]=g_tq_p_def + method tq_m() { + templates.tq.tq_m(); // @goto-def-decl[19]->tq_def @goto-def-decl[22]->tq_m_def + } + + method uses() { + // @loc[25]=c_decl @goto-def-decl[25]->c_decl @goto-type-def[25]->int_alias_def + local int_alias c = C; // @goto-def-decl[29]->constant_def + // @loc[25]=la_decl @goto-def-decl[25]->la_decl @goto-type-def[25]->int_alias_def + local int_alias la = s_lay.la + s_bf.bf; // @goto-def-decl[36]->layout_field_def @goto-def-decl[46]->bitfield_def + // @loc[22]=lg_a_use + log info, 1, lg_a: "x"; // @goto-def-decl[22]->loggroup_def + after h: target(); // @goto-def-decl[15]->hook_def @goto-def-decl[18]->target_def + #foreach v in ([1, 2]) { // @loc[18]=foreach_v_def + // @loc[29]=y_decl @goto-def-decl[29]->y_decl @goto-type-def[29]->int_alias_def + local int_alias y = v; // @goto-def-decl[33]->foreach_v_def + } + foreach o in (each tq in (this)) { // @loc[17]=foreach_o_def @goto-def-decl[28]->tq_def + // @loc[29]=z_decl @goto-def-decl[29]->z_decl @goto-type-def[29]->int_alias_def + local int_alias z = o.tq_p; // @goto-def-decl[33]->foreach_o_def @goto-def[35]->g_tq_p_def @goto-decl[35]->tq_p_def + o.tq_s(); // @goto-def-decl[15]->tq_s_def + } + // @loc[17]=select_s_def @goto-def-decl[17]->select_s_def @goto-type-def[17]->tq_def + #select s in (each tq in (this)) where (true) { + s.tq_s(); // @goto-def-decl[13]->select_s_def @goto-def-decl[15]->tq_s_def + } #else { + } + tq_ref.tq_s(); // @goto-def-decl[16]->tq_s_def + // @loc[25]=sz_decl @goto-def-decl[25]->sz_decl @goto-type-def[25]->int_alias_def + local int_alias sz = sizeoftype(lay_t); // @goto-def-decl[41]->lay_t_def + } +} diff --git a/tests/test_files/lookup_scopes.dml b/tests/test_files/lookup_scopes.dml new file mode 100644 index 00000000..684fb522 --- /dev/null +++ b/tests/test_files/lookup_scopes.dml @@ -0,0 +1,94 @@ +dml 1.4; +device lookup_scopes; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def + +// Block scoping of locals, following DMLC (which follows C for 'switch') + +hook(int, bool) h; +// @loc[25]=i_decl @goto-def-decl[25]->i_decl @goto-type-def[25]->int_alias_def @loc[39]=b_decl @goto-def-decl[39]->b_decl @goto-type-def[39]->bool_alias_def +method target(int_alias i, bool_alias b) {} // @loc[8]=target_def + +// @loc[32]=sel_decl @goto-def-decl[32]->sel_decl @goto-type-def[32]->int_alias_def +method switch_locals(int_alias sel) -> (int) { + switch (sel) { + case 0: + // @goto-def-decl[25]->sw_decl @goto-type-def[25]->int_alias_def + local int_alias sw; // @loc[25]=sw_decl + sw = 1; // @goto-def-decl[9]->sw_decl + break; + case 1: + // Later cases see locals of earlier ones + sw = 2; // @goto-def-decl[9]->sw_decl + break; + #if (true) { + case 2: + // @goto-def-decl[25]->in_hashif_decl @goto-type-def[25]->int_alias_def + local int_alias in_hashif; // @loc[25]=in_hashif_decl + in_hashif = 3; // @goto-def-decl[9]->in_hashif_decl + break; + } + default: { + // @goto-def-decl[25]->nested_decl @goto-type-def[25]->int_alias_def + local int_alias nested; // @loc[25]=nested_decl + nested = 4; // @goto-def-decl[9]->nested_decl + } + } + // Locals declared directly in a switch body leak into the enclosing block + sw = nested; // @goto-def-decl[5]->sw_decl @goto-def-decl[10]-> + return sw; // @goto-def-decl[12]->sw_decl +} + +// @loc[35]=sel_decl_2 @goto-def-decl[35]->sel_decl_2 @goto-type-def[35]->int_alias_def +method switch_redeclare(int_alias sel) { + switch (sel) { + case 0: + // @goto-def-decl[25]->leaked_decl @goto-type-def[25]->int_alias_def + local int_alias leaked; // @loc[25]=leaked_decl + break; + } + // @diag-error: Duplicate definition of variable 'leaked' | related->leaked_decl: Previously defined here + // @loc[21]=leaked_decl_2 @goto-def-decl[21]->leaked_decl_2 @goto-type-def[21]->int_alias_def + local int_alias leaked; +} + +method blocks() { + { + // @goto-def-decl[25]->inner_decl @goto-type-def[25]->int_alias_def + local int_alias inner; // @loc[25]=inner_decl + inner = 1; // @goto-def-decl[9]->inner_decl + } + // A nested block does not leak, so this is no redefinition + // @goto-def-decl[21]->outer_inner_decl @goto-type-def[21]->int_alias_def + local int_alias inner; // @loc[21]=outer_inner_decl + inner = 2; // @goto-def-decl[5]->outer_inner_decl +} + +// @goto-def-decl[29]->arg_decl @goto-type-def[29]->int_alias_def +method duplicates(int_alias arg) { // @loc[29]=arg_decl + { + // @loc[25]=arg_decl_2 @goto-def-decl[25]->arg_decl_2 @goto-type-def[25]->int_alias_def + local int_alias arg; + } + // @diag-error: Duplicate definition of variable 'arg' | related->arg_decl: Previously defined here + // @loc[21]=arg_decl_3 @goto-def-decl[21]->arg_decl_3 @goto-type-def[21]->int_alias_def + local int_alias arg; + // @goto-def-decl[21]->twice_first @goto-type-def[21]->int_alias_def + local int_alias twice; // @loc[21]=twice_first + // @diag-error: Duplicate definition of variable 'twice' | related->twice_first: Previously defined here + // @loc[21]=twice_decl @goto-def-decl[21]->twice_decl @goto-type-def[21]->int_alias_def + local int_alias twice; +} + +method hooks() { + // @goto-def-decl[21]->local_i @goto-type-def[21]->int_alias_def + local int_alias i = 0; // @loc[21]=local_i + // @loc[17]=bind_i + // @loc[20]=bind_b + after h -> (i, b): target(i, b); // @goto-def-decl[24]->target_def @goto-def-decl[31]->bind_i @goto-def-decl[34]->bind_b + // @loc[20]=discard_bind_b + after h -> (_, b): target(i, b); // @goto-def-decl[31]->local_i @goto-def-decl[34]->discard_bind_b + // @diag-error: Duplicate definition of variable 'p' | related->first_p: Previously defined here + after h -> (p, p): target(p, false); // @loc[17]=first_p +} diff --git a/tests/test_files/multi_level.dml b/tests/test_files/multi_level.dml index 57c66fac..c94f4e8a 100644 --- a/tests/test_files/multi_level.dml +++ b/tests/test_files/multi_level.dml @@ -4,6 +4,8 @@ dml 1.4; device multi_level_test; +typedef int int_alias; // @loc[13]=int_alias_def + // Test multiple levels of template inheritance (3 levels) // @loc[10]=shared_level_template template shared_level_template { // @goto-ref[10]->level_0_template_is_shared @@ -73,9 +75,10 @@ bank multi_instance_bank { param upwards_param = 2; method use_param() { // Reference to template param - goto-def should go to template - // @loc[27]=reg1_this_ref - // @loc[32]=reg1_level_0_param_ref - local int x = this.level_0_param; // @goto-def[37]->level_2_level_0_param @goto-decl[37]->level_0_param + // @loc[33]=reg1_this_ref + // @loc[38]=reg1_level_0_param_ref + // @loc[29]=x_decl @goto-def-decl[29]->x_decl @goto-type-def[29]->int_alias_def + local int_alias x = this.level_0_param; // @goto-def[43]->level_2_level_0_param @goto-decl[43]->level_0_param // @loc[18]=reg_1_level_1_param_ref; x += level_1_param; // @goto-def[18]->level_2_level_1_param @goto-decl[18]->level_1_param // @loc[18]=reg_1_level_2_param_ref; @@ -89,9 +92,10 @@ bank multi_instance_bank { param upwards_param = 4; method use_param() { // Reference to template param - goto-def should go to template - // @loc[27]=reg2_this_ref - // @loc[32]=reg2_level_0_param_ref - local int x = this.level_0_param; // @goto-def[37]->level_2_level_0_param @goto-decl[37]->level_0_param + // @loc[33]=reg2_this_ref + // @loc[38]=reg2_level_0_param_ref + // @loc[29]=x_decl_2 @goto-def-decl[29]->x_decl_2 @goto-type-def[29]->int_alias_def + local int_alias x = this.level_0_param; // @goto-def[43]->level_2_level_0_param @goto-decl[43]->level_0_param // @loc[18]=reg_2_level_1_param_ref; x += level_1_param; // @goto-def[18]->level_2_level_1_param @goto-decl[18]->level_1_param // @loc[18]=reg_2_level_2_param_ref; diff --git a/tests/test_files/repeated_externs.dml b/tests/test_files/repeated_externs.dml index c032860d..ed2293ba 100644 --- a/tests/test_files/repeated_externs.dml +++ b/tests/test_files/repeated_externs.dml @@ -3,6 +3,8 @@ dml 1.4; device repeated_externs; + +typedef int int_alias; // @loc[13]=int_alias_def import "repeated_extern_import.dml"; extern int shared_value; // @loc[12]=value @goto-ref[12]->value_use @@ -11,9 +13,11 @@ extern int shared_function(int argument); // @loc[12]=function extern int shared_function(int renamed_argument); // @loc[12]=function_again method use_externs() -> (int) { - local int result = shared_value; // @loc[24]=value_use @goto-def-decl[24]->value,value_again,repeated_extern_import.dml:imported_value + // @loc[21]=result_decl @goto-def-decl[21]->result_decl @goto-type-def[21]->int_alias_def + local int_alias result = shared_value; // @loc[30]=value_use @goto-def-decl[30]->value,value_again,repeated_extern_import.dml:imported_value return shared_function(result); // @goto-def-decl[12]->function,function_again,repeated_extern_import.dml:imported_function } extern int colliding_name; +// Loses the name collision to the extern, so has no symbol of its own session int colliding_name; diff --git a/tests/test_files/type_arrays.dml b/tests/test_files/type_arrays.dml new file mode 100644 index 00000000..8f8333ae --- /dev/null +++ b/tests/test_files/type_arrays.dml @@ -0,0 +1,21 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_arrays; + +typedef int int_alias; // @loc[13]=int_alias_def + +bank diagnostics { + method arrays() { + // @loc[25]=valid_array_decl @goto-def-decl[25]->valid_array_decl @goto-type-def[25]->int_alias_def + local int_alias valid_array[3] = {0, 1, 2}; + // @diag-error: initialize element 1 of local array 'invalid_array' | related->invalid_array_decl: declared as 'int_alias[3]' here + // @diag-error: initialize element 2 of local array 'invalid_array' | related->invalid_array_decl: declared as 'int_alias[3]' here + // @goto-def-decl[25]->invalid_array_decl @goto-type-def[25]->int_alias_def + local int_alias invalid_array[3] = {0, false, "array"}; // @loc[25]=invalid_array_decl + // @diag-error: Local array 'short_array' has length 3 | related->short_array_decl: Local array 'short_array' declared here + // @goto-def-decl[25]->short_array_decl @goto-type-def[25]->int_alias_def + local int_alias short_array[3] = {0, 1}; // @loc[25]=short_array_decl + } +} diff --git a/tests/test_files/type_assignments.dml b/tests/test_files/type_assignments.dml new file mode 100644 index 00000000..16396671 --- /dev/null +++ b/tests/test_files/type_assignments.dml @@ -0,0 +1,107 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_assignments; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef float float_alias; // @loc[15]=float_alias_def +typedef int int_alias; // @loc[13]=int_alias_def +typedef int8 int8_alias; // @loc[14]=int8_alias_def +typedef uint16 uint16_alias; // @loc[16]=uint16_alias_def +typedef uint8 uint8_alias; // @loc[15]=uint8_alias_def + +typedef bitfields 8 { + uint1 flag @ [0]; +// @loc[3]=byte_bits_t_def +} byte_bits_t; + +typedef bitfields 1 {} one_bit_t; +typedef bitfields 64 {} sixty_four_bits_t; +typedef bitfields 6_4 {} underscored_width_t; + +bank diagnostics { + // @loc[45]=byte_decl @goto-def-decl[45]->byte_decl @goto-type-def[45]->uint8_alias_def @loc[64]=wide_decl @goto-def-decl[64]->wide_decl @goto-type-def[64]->uint16_alias_def + method bitfield_assignments(uint8_alias byte, uint16_alias wide) { + // @loc[27]=from_literal_decl @goto-def-decl[27]->from_literal_decl @goto-type-def[27]->byte_bits_t_def + local byte_bits_t from_literal = 1; + // @loc[27]=from_integer_decl @goto-def-decl[27]->from_integer_decl @goto-type-def[27]->byte_bits_t_def + local byte_bits_t from_integer = byte; + // @loc[27]=from_wide_integer_decl @goto-def-decl[27]->from_wide_integer_decl @goto-type-def[27]->byte_bits_t_def + local byte_bits_t from_wide_integer = wide; + // @loc[27]=from_bitfields_decl @goto-def-decl[27]->from_bitfields_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias from_bitfields = from_integer; + // @diag-error: invalid_bitfields_assignment | related->invalid_bitfields_assignment_decl: declared as 'byte_bits_t' here + // @goto-def-decl[27]->invalid_bitfields_assignment_decl @goto-type-def[27]->byte_bits_t_def + local byte_bits_t invalid_bitfields_assignment = 1.5; // @loc[27]=invalid_bitfields_assignment_decl + } + + method assignments() { + // @loc[26]=tuple_int_decl @goto-def-decl[26]->tuple_int_decl @goto-type-def[26]->int_alias_def @loc[48]=tuple_bool_decl @goto-def-decl[48]->tuple_bool_decl @goto-type-def[48]->bool_alias_def + local (int_alias tuple_int, bool_alias tuple_bool) = (4, false); + // @diag-error: bad_tuple_int | related->bad_tuple_int_decl: declared as 'int_alias' here + // @diag-error: bad_tuple_bool | related->bad_tuple_bool_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_tuple_int_decl @goto-type-def[26]->int_alias_def @goto-def-decl[52]->bad_tuple_bool_decl @goto-type-def[52]->bool_alias_def + local (int_alias bad_tuple_int, bool_alias bad_tuple_bool) = ("tuple", 4); // @loc[26]=bad_tuple_int_decl @loc[52]=bad_tuple_bool_decl + // @loc[27]=const_tuple_bool_decl @goto-def-decl[27]->const_tuple_bool_decl @goto-type-def[27]->bool_alias_def @loc[61]=const_tuple_int_decl @goto-def-decl[61]->const_tuple_int_decl @goto-type-def[61]->int_alias_def + local (bool_alias const_tuple_bool, const int_alias const_tuple_int) = (false, 4); + // @loc[27]=bad_const_tuple_bool_decl @goto-def-decl[27]->bad_const_tuple_bool_decl @goto-type-def[27]->bool_alias_def @goto-def-decl[65]->bad_const_tuple_int_decl @goto-type-def[65]->int_alias_def + local (bool_alias bad_const_tuple_bool, const int_alias bad_const_tuple_int) = // @loc[65]=bad_const_tuple_int_decl + // @diag-error: bad_const_tuple_int | related->bad_const_tuple_int_decl: declared as 'const int_alias' here + (false, "const tuple"); + + // @loc[25]=simultaneous_int_decl @goto-def-decl[25]->simultaneous_int_decl @goto-type-def[25]->int_alias_def + local int_alias simultaneous_int; + // @loc[26]=simultaneous_bool_decl @goto-def-decl[26]->simultaneous_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias simultaneous_bool; + (simultaneous_int, simultaneous_bool) = (4, false); + // @goto-def-decl[25]->bad_simultaneous_int_decl @goto-type-def[25]->int_alias_def + local int_alias bad_simultaneous_int; // @loc[25]=bad_simultaneous_int_decl + // @goto-def-decl[26]->bad_simultaneous_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_simultaneous_bool; // @loc[26]=bad_simultaneous_bool_decl + // @diag-error: bad_simultaneous_int | related->bad_simultaneous_int_decl: declared as 'int_alias' here + // @diag-error: bad_simultaneous_bool | related->bad_simultaneous_bool_decl: declared as 'bool_alias' here + (bad_simultaneous_int, bad_simultaneous_bool) = ("simultaneous", 4); + // @loc[25]=short_simultaneous_int_decl @goto-def-decl[25]->short_simultaneous_int_decl @goto-type-def[25]->int_alias_def + local int_alias short_simultaneous_int; + // @loc[26]=short_simultaneous_bool_decl @goto-def-decl[26]->short_simultaneous_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias short_simultaneous_bool; + // @loc[25]=short_simultaneous_extra_decl @goto-def-decl[25]->short_simultaneous_extra_decl @goto-type-def[25]->int_alias_def + local int_alias short_simultaneous_extra; + // @diag-error: has 3 targets but initializer provides 2 values + (short_simultaneous_int, short_simultaneous_bool, + short_simultaneous_extra) = (4, false); + // @loc[25]=long_simultaneous_int_decl @goto-def-decl[25]->long_simultaneous_int_decl @goto-type-def[25]->int_alias_def + local int_alias long_simultaneous_int; + // @loc[26]=long_simultaneous_bool_decl @goto-def-decl[26]->long_simultaneous_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias long_simultaneous_bool; + // @diag-error: has 2 targets but initializer provides 3 values + (long_simultaneous_int, long_simultaneous_bool) = (4, false, 8); + // @diag-error: has 2 targets but initializer provides 1 value + (long_simultaneous_int, long_simultaneous_bool) = 4; + (long_simultaneous_int, long_simultaneous_bool) = unknown_pair(); + + // @goto-def-decl[25]->chain_int_decl @goto-type-def[25]->int_alias_def + local int_alias chain_int; // @loc[25]=chain_int_decl + // @loc[26]=chain_bool_decl @goto-def-decl[26]->chain_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias chain_bool; + // @diag-error: Cannot assign value of type 'bool_alias' | related->chain_int_decl: declared as 'int_alias' here + chain_int = chain_bool = false; + + // @loc[25]=swap_i_decl @goto-def-decl[25]->swap_i_decl @goto-type-def[25]->int_alias_def + local int_alias swap_i = 1; + // @loc[25]=swap_j_decl @goto-def-decl[25]->swap_j_decl @goto-type-def[25]->int_alias_def + local int_alias swap_j = 0; + (swap_i, swap_j) = (swap_j, swap_i); + + // @loc[26]=chain_byte_decl @goto-def-decl[26]->chain_byte_decl @goto-type-def[26]->int8_alias_def + local int8_alias chain_byte; // @disabled-location[26]=chain_byte_decl + // @loc[27]=chain_float_decl @goto-def-decl[27]->chain_float_decl @goto-type-def[27]->float_alias_def + local float_alias chain_float; + // @loc[25]=chain_integer_decl @goto-def-decl[25]->chain_integer_decl @goto-type-def[25]->int_alias_def + local int_alias chain_integer; // @disabled-location[25]=chain_integer_decl + // @disabled-diag-warning: chain_byte | related->chain_byte_decl: declared as 'int8' here + // @disabled-diag-warning: chain_integer | related->chain_integer_decl: declared as 'int' here + chain_float = chain_byte = chain_integer = 255.1; + } +} diff --git a/tests/test_files/type_default_calls.dml b/tests/test_files/type_default_calls.dml new file mode 100644 index 00000000..c5cfb269 --- /dev/null +++ b/tests/test_files/type_default_calls.dml @@ -0,0 +1,59 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_default_calls; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def + +template assignment_base { + method checked_without_default_call() default { + // @diag-error: base_without_default_assignment | related->base_without_default_decl: declared as 'int_alias' here + // @goto-def-decl[25]->base_without_default_decl @goto-type-def[25]->int_alias_def + local int_alias base_without_default_assignment = "base without default"; // @loc[25]=base_without_default_decl + } + + // @goto-def-decl[48]->checked_default_arg @goto-type-def[48]->int_alias_def + method checked_with_default_call(int_alias default_argument) -> (int, bool) // @loc[12]=checked_default_method @loc[48]=checked_default_arg + default { + // @diag-error: base_with_default_assignment | related->base_with_default_decl: declared as 'int_alias' here + // @goto-def-decl[25]->base_with_default_decl @goto-type-def[25]->int_alias_def + local int_alias base_with_default_assignment = "base with default"; // @loc[25]=base_with_default_decl + return (default_argument, false); + } +} + +template assignment_override is assignment_base { + method checked_without_default_call() { + // @diag-error: override_without_default_assignment | related->override_without_default_decl: declared as 'int_alias' here + // @goto-def-decl[25]->override_without_default_decl @goto-type-def[25]->int_alias_def + local int_alias override_without_default_assignment = false; // @loc[25]=override_without_default_decl + } + + // @loc[48]=default_argument_decl @goto-def-decl[48]->default_argument_decl @goto-type-def[48]->int_alias_def + method checked_with_default_call(int_alias default_argument) -> (int, bool) { + // @loc[26]=default_int_decl @goto-def-decl[26]->default_int_decl @goto-type-def[26]->int_alias_def @loc[50]=default_bool_decl @goto-def-decl[50]->default_bool_decl @goto-type-def[50]->bool_alias_def + local (int_alias default_int, bool_alias default_bool) = default(default_argument); + // @goto-def-decl[27]->bad_default_bool_decl @goto-type-def[27]->bool_alias_def @goto-def-decl[55]->bad_default_int_decl @goto-type-def[55]->int_alias_def + local (bool_alias bad_default_bool, int_alias bad_default_int) = // @loc[27]=bad_default_bool_decl @loc[55]=bad_default_int_decl + // @diag-error: bad_default_bool | related->bad_default_bool_decl: declared here + // @diag-error: bad_default_int | related->bad_default_int_decl: declared here + // @diag-error: Cannot pass value of type 'const char *' | related->checked_default_arg: Argument declared here + default("default argument"); + // @loc[26]=missing_default_arg_int_decl @goto-def-decl[26]->missing_default_arg_int_decl @goto-type-def[26]->int_alias_def @loc[62]=missing_default_arg_bool_decl @goto-def-decl[62]->missing_default_arg_bool_decl @goto-type-def[62]->bool_alias_def + local (int_alias missing_default_arg_int, bool_alias missing_default_arg_bool) = + // @diag-error: expects 1 arguments but call provides 0 | related->checked_default_method: Method declared here + default(); + // @diag-error: returns 2 values but declaration binds 1 | related->checked_default_method: Method declared here + // @loc[25]=short_default_binding_decl @goto-def-decl[25]->short_default_binding_decl @goto-type-def[25]->int_alias_def + local int_alias short_default_binding = default(default_argument); + // @diag-error: override_with_default_assignment | related->override_with_default_decl: declared as 'int_alias' here + // @goto-def-decl[25]->override_with_default_decl @goto-type-def[25]->int_alias_def + local int_alias override_with_default_assignment = false; // @loc[25]=override_with_default_decl + return default(default_argument); + } +} + +bank diagnostics is assignment_override { +} diff --git a/tests/test_files/type_definitions.dml b/tests/test_files/type_definitions.dml new file mode 100644 index 00000000..b0cdfbcd --- /dev/null +++ b/tests/test_files/type_definitions.dml @@ -0,0 +1,60 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_definitions; + +typedef int int_alias; // @loc[13]=int_alias_def + +// Go-to-definition finds the declaration of a name, while +// go-to-type-definition finds the declaration of its type + +typedef uint32 addr_t; // @loc[16]=addr_t_def + +typedef struct { + addr_t x; // @loc[12]=x_def +} point_t; // @loc[3]=point_t_def + +extern typedef struct { + void (*ping)(int value); +} some_interface_interface_t; // @loc[3]=interface_t_def + +// @goto-def-decl[11]->implement_def @goto-type-def[11]->interface_t_def +implement some_interface { // @loc[11]=implement_def + session addr_t foo; // @loc[20]=foo_def @goto-def-decl[20]->foo_def @goto-type-def[20]->addr_t_def + // @loc[27]=value_decl @goto-def-decl[27]->value_decl @goto-type-def[27]->int_alias_def + method ping(int_alias value) {} +} + +template typed_t { // @loc[10]=typed_t_def + param tp : addr_t; // @loc[11]=tp_decl +} + +group holder is typed_t { // @loc[7]=holder_def + param tp = 4; // @loc[11]=tp_def +} + +// @goto-def-decl[17]->sp_def @goto-type-def[17]->point_t_def +session point_t sp; // @loc[17]=sp_def +// @goto-def-decl[14]->sa_def @goto-type-def[14]->addr_t_def +saved addr_t sa; // @loc[14]=sa_def +extern addr_t ea; // @loc[15]=ea_def +// @goto-def-decl[17]->tref_def @goto-type-def[17]->typed_t_def +session typed_t tref; // @loc[17]=tref_def + +// @goto-def-decl[20]->arg_def @goto-type-def[20]->addr_t_def +method uses(addr_t arg) { // @loc[8]=uses_def @loc[20]=arg_def + // @goto-def-decl[15]->plain_def @goto-type-def[15]-> + local int plain = 0; // @loc[15]=plain_def + // @goto-def-decl[18]->typed_def @goto-type-def[18]->addr_t_def + local addr_t typed = 0; // @loc[18]=typed_def + plain = 1; // @goto-def-decl[5]->plain_def @goto-type-def[5]-> + typed = arg; // @goto-def-decl[5]->typed_def @goto-type-def[5]->addr_t_def @goto-def-decl[13]->arg_def @goto-type-def[13]->addr_t_def + typed = sa + ea; // @goto-def-decl[13]->sa_def @goto-type-def[13]->addr_t_def @goto-def-decl[18]->ea_def @goto-type-def[18]->addr_t_def + typed = sp.x; // @goto-def-decl[13]->sp_def @goto-type-def[13]->point_t_def @goto-def-decl[16]->x_def @goto-type-def[16]->addr_t_def + typed = some_interface.foo; // @goto-def-decl[13]->implement_def @goto-type-def[13]->interface_t_def @goto-def-decl[28]->foo_def @goto-type-def[28]->addr_t_def + typed = holder.tp; // @goto-def-decl[13]->holder_def @goto-type-def[13]-> @goto-def[20]->tp_def @goto-decl[20]->tp_decl @goto-type-def[20]->addr_t_def + // @loc[19]=t2_decl @goto-def-decl[19]->t2_decl @goto-type-def[19]->typed_t_def + local typed_t t2 = tref; // @goto-def-decl[24]->tref_def @goto-type-def[24]->typed_t_def + uses(typed); // @goto-def-decl[5]->uses_def @goto-type-def[5]-> @goto-type-def[10]->addr_t_def +} diff --git a/tests/test_files/type_interfaces.dml b/tests/test_files/type_interfaces.dml new file mode 100644 index 00000000..d14ddbff --- /dev/null +++ b/tests/test_files/type_interfaces.dml @@ -0,0 +1,24 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_interfaces; + +extern typedef struct { + // @loc[12]=some_interface_method_def + // @goto-impl[12]->some_interface_method_impl + void (*interface_method)(conf_object_t *obj); +// @loc[3]=some_interface_t_def +// @goto-ref[3]->some_interface_t_ref +// @goto-impl[3]->some_interface_t_ref +} some_interface_t; + +// @loc[11]=some_interface_t_ref +// @goto-def-decl[11]->some_impl_def +// @goto-type-def[11]->some_interface_t_def +implement some { // @loc[11]=some_impl_def + // @loc[12]=some_interface_method_impl + // @goto-decl[12]->some_interface_method_def + // @goto-def[12]->some_interface_method_impl + method interface_method() {} +} diff --git a/tests/test_files/type_member_access.dml b/tests/test_files/type_member_access.dml new file mode 100644 index 00000000..4e005bb2 --- /dev/null +++ b/tests/test_files/type_member_access.dml @@ -0,0 +1,213 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_member_access; + +typedef uint32 uint32_alias; // @loc[16]=uint32_alias_def + +typedef struct { + // @loc[12]=x_field_def + uint32 x; +// @loc[3]=point_t_def +} point_t; + +typedef struct { + point_t named; + struct { + // @loc[16]=y_field_def + uint32 y; + } anonymous; +// @loc[3]=line_t_def +} line_t; + +// @loc[16]=sess_line_decl @goto-def-decl[16]->sess_line_decl @goto-type-def[16]->line_t_def +session line_t sess_line; + +method use_nested() -> (uint32) { + return sess_line.named.x + sess_line.anonymous.y; // @goto-def-decl[28]->x_field_def @goto-def-decl[52]->y_field_def +} + +session struct { // @loc[9]=sess_inline_struct + // @loc[12]=z_field_def + uint32 z; + struct { + // @loc[16]=w_field_def + uint32 w; + } inner; +} sess_inline; // @loc[3]=sess_inline_decl @goto-def-decl[3]->sess_inline_decl @goto-type-def[3]->sess_inline_struct + +method use_inline() -> (uint32) { + local struct { // @loc[11]=loc_inline_struct + // @loc[16]=v_field_def + uint32 v; + } loc_inline; // @loc[7]=loc_inline_decl @goto-def-decl[7]->loc_inline_decl @goto-type-def[7]->loc_inline_struct + return sess_inline.z + sess_inline.inner.w + loc_inline.v; // @goto-def-decl[24]->z_field_def @goto-def-decl[46]->w_field_def @goto-def-decl[61]->v_field_def +} + +extern point_t ext_point; +// @loc[17]=sess_point_decl @goto-def-decl[17]->sess_point_decl @goto-type-def[17]->point_t_def +session point_t sess_point; +// @loc[15]=saved_point_decl @goto-def-decl[15]->saved_point_decl @goto-type-def[15]->point_t_def +saved point_t saved_point; + +template has_point { + param tpoint : point_t; + shared method read_tpoint() -> (uint32) { + return tpoint.x; // @goto-def-decl[23]->x_field_def + } +} + +group g is has_point { + param tpoint = saved_point; + method read_param() -> (uint32) { + return tpoint.x; // @goto-def-decl[23]->x_field_def + } +} + +// @loc[28]=arg_decl @goto-def-decl[28]->arg_decl @goto-type-def[28]->point_t_def +method use_members(point_t arg) -> (uint32) { + // @loc[19]=loc_decl @goto-def-decl[19]->loc_decl @goto-type-def[19]->point_t_def + local point_t loc; + loc.x = arg.x + ext_point.x + sess_point.x + saved_point.x; // @goto-def-decl[9]->x_field_def @goto-def-decl[17]->x_field_def @goto-def-decl[31]->x_field_def @goto-def-decl[46]->x_field_def @goto-def-decl[62]->x_field_def + return loc.x; +} + +// Chains through pointers, an anonymous struct and several typedefs + +typedef struct { + // @loc[12]=chain_e_def + uint32 e; +// @loc[3]=chain_leaf_t_def +} chain_leaf_t; + +typedef struct { + struct { + // @loc[23]=chain_d_def + chain_leaf_t *d; + // @loc[7]=chain_c_def + } c; +} chain_middle_t; + +typedef struct { + // @loc[21]=chain_b_def + chain_middle_t *b; +// @loc[3]=chain_outer_t_def +} chain_outer_t; + +typedef chain_middle_t *chain_middle_ptr_t; +typedef chain_leaf_t chain_leaf_alias_t; + +typedef struct { + // @loc[24]=via_ptr_typedef_def + chain_middle_ptr_t via_ptr_typedef; + // @loc[24]=via_alias_def + chain_leaf_alias_t via_alias; +// @loc[3]=chain_indirect_t_def +} chain_indirect_t; + +// @loc[26]=indirect_decl @goto-def-decl[26]->indirect_decl @goto-type-def[26]->chain_indirect_t_def +session chain_indirect_t indirect; + +// @loc[37]=a_decl @goto-def-decl[37]->a_decl @goto-type-def[37]->chain_outer_t_def +method chain_by_value(chain_outer_t a) -> (uint32) { + return a.b->c.d->e; // @goto-def-decl[14]->chain_b_def @goto-def-decl[17]->chain_c_def @goto-def-decl[19]->chain_d_def @goto-def-decl[22]->chain_e_def +} + +// @loc[40]=ap_decl @goto-def-decl[40]->ap_decl @goto-type-def[40]->chain_outer_t_def +method chain_by_pointer(chain_outer_t *ap) -> (uint32) { + return ap->b->c.d->e; // @goto-def-decl[16]->chain_b_def @goto-def-decl[19]->chain_c_def @goto-def-decl[21]->chain_d_def @goto-def-decl[24]->chain_e_def +} + +method chain_through_typedefs() -> (uint32) { + return indirect.via_ptr_typedef->c.d->e + indirect.via_alias.e; // @goto-def-decl[21]->via_ptr_typedef_def @goto-def-decl[38]->chain_c_def @goto-def-decl[40]->chain_d_def @goto-def-decl[43]->chain_e_def @goto-def-decl[56]->via_alias_def @goto-def-decl[66]->chain_e_def +} + +typedef struct { + // @loc[19]=entries_def + chain_outer_t entries[2]; + struct { + // @loc[16]=slot_value_def + uint32 value; + // @loc[7]=slots_def + } slots[4]; +// @loc[3]=chain_table_t_def +} chain_table_t; + +// @loc[36]=tables_decl @goto-def-decl[36]->tables_decl @goto-type-def[36]->chain_table_t_def +method chain_arrays(chain_table_t *tables) -> (uint32) { + // @loc[24]=leaves_decl @goto-def-decl[24]->leaves_decl @goto-type-def[24]->chain_leaf_t_def + local chain_leaf_t leaves[4]; + // @loc[24]=sum_def + // @goto-def-decl[24]->sum_def @goto-type-def[24]->uint32_alias_def + local uint32_alias sum = leaves[1].e; // @goto-def-decl[40]->chain_e_def + sum = leaves[sum].e; // @goto-def-decl[18]->sum_def @goto-def-decl[23]->chain_e_def + sum = tables[0].entries[1].b->c.d[0].e; // @goto-def-decl[21]->entries_def @goto-def-decl[32]->chain_b_def @goto-def-decl[35]->chain_c_def @goto-def-decl[37]->chain_d_def @goto-def-decl[42]->chain_e_def + sum = tables->slots[2].value; // @goto-def-decl[19]->slots_def @goto-def-decl[28]->slot_value_def + return sum; +} + +// Member access must match the pointer/array layers of the value +method chain_access_checks() -> (uint32) { + // @loc[26]=ptrs_decl @goto-def-decl[26]->ptrs_decl @goto-type-def[26]->chain_outer_t_def + local chain_outer_t *ptrs[2]; + // @loc[24]=leaves_decl_2 @goto-def-decl[24]->leaves_decl_2 @goto-type-def[24]->chain_leaf_t_def + local chain_leaf_t leaves[2]; + // @loc[25]=leaf_ptr_decl @goto-def-decl[25]->leaf_ptr_decl @goto-type-def[25]->chain_leaf_t_def + local chain_leaf_t *leaf_ptr; + // @loc[24]=sum_decl @goto-def-decl[24]->sum_decl @goto-type-def[24]->uint32_alias_def + local uint32_alias sum = ptrs[0]->b->c.d->e; // @goto-def-decl[39]->chain_b_def @goto-def-decl[42]->chain_c_def @goto-def-decl[44]->chain_d_def @goto-def-decl[47]->chain_e_def + sum = ptrs[0][0].b->c.d[0].e; // @goto-def-decl[22]->chain_b_def + sum = leaves->e; // @goto-def-decl[19]->chain_e_def + sum = leaf_ptr[0].e; // @goto-def-decl[23]->chain_e_def + sum = leaves.e; // @goto-def-decl[18]-> + sum = ptrs[0].b; // @goto-def-decl[19]-> + sum = leaf_ptr.e; // @goto-def-decl[20]-> + sum = leaves[0]->e; // @goto-def-decl[22]-> + sum = indirect.via_ptr_typedef.c; // @goto-def-decl[36]-> + return sum; +} + +// Indexing into arrays of structs before member access + +typedef chain_leaf_t chain_leaf_array_t[4]; + +typedef struct { + // @loc[18]=leaves_def + chain_leaf_t leaves[4]; + // @loc[19]=leaf_ptrs_def + chain_leaf_t *leaf_ptrs[2]; + struct { + // @loc[16]=nested_n_def + uint32 n; + // @loc[7]=nested_def + } nested[3]; + // @loc[24]=typedef_array_def + chain_leaf_array_t typedef_array; +// @loc[3]=chain_arrays_t_def +} chain_arrays_t; + +// @loc[24]=arrays_decl @goto-def-decl[24]->arrays_decl @goto-type-def[24]->chain_arrays_t_def +session chain_arrays_t arrays; +// @loc[24]=array_of_arrays_decl @goto-def-decl[24]->array_of_arrays_decl @goto-type-def[24]->chain_arrays_t_def +session chain_arrays_t array_of_arrays[2]; + +// @loc[36]=i_decl @goto-def-decl[36]->i_decl @goto-type-def[36]->uint32_alias_def +method chain_indexing(uint32_alias i) -> (uint32) { + // @loc[24]=r_decl @goto-def-decl[24]->r_decl @goto-type-def[24]->uint32_alias_def + local uint32_alias r = arrays.leaves[i].e; // @goto-def-decl[35]->leaves_def @goto-def-decl[45]->chain_e_def + r += arrays.leaf_ptrs[0]->e; // @goto-def-decl[17]->leaf_ptrs_def @goto-def-decl[31]->chain_e_def + r += arrays.nested[i].n; // @goto-def-decl[17]->nested_def @goto-def-decl[27]->nested_n_def + r += arrays.typedef_array[1].e; // @goto-def-decl[17]->typedef_array_def @goto-def-decl[34]->chain_e_def + r += array_of_arrays[i].leaves[0].e; // @goto-def-decl[29]->leaves_def @goto-def-decl[39]->chain_e_def + return r; +} + +method point_ptr() -> (point_t *) { + return &sess_point; +} + +// Members of non-reference expressions are not resolved (a known limitation) +method untracked_member() -> (uint32) { + return point_ptr()->x; // @goto-def-decl[25]-> +} diff --git a/tests/test_files/type_method_calls.dml b/tests/test_files/type_method_calls.dml new file mode 100644 index 00000000..a49b3679 --- /dev/null +++ b/tests/test_files/type_method_calls.dml @@ -0,0 +1,122 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_method_calls; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def +typedef uint8 uint8_alias; // @loc[15]=uint8_alias_def + +typedef struct { + struct { int value; } nested; +// @loc[3]=nested_container_t_def +} nested_container_t; + +extern typedef struct { + struct { int value; } nested; +// @loc[3]=extern_nested_container_t_def +} extern_nested_container_t; + +group typed_object { + // @goto-def-decl[34]->object_pair_argument @goto-type-def[34]->int_alias_def @goto-def-decl[55]->object_pair_flag @goto-type-def[55]->bool_alias_def + method object_pair(int_alias argument, bool_alias flag) -> (int, bool) { // @loc[12]=object_pair_method @loc[34]=object_pair_argument @loc[55]=object_pair_flag @goto-ref[12]->object_pair_bad_arg_call,object_pair_short_call,object_pair_bad_bind_call + return (argument, flag); + } +} + +bank diagnostics { + // @loc[57]=regular_value_decl @goto-def-decl[57]->regular_value_decl @goto-type-def[57]->nested_container_t_def + method accepts_nested_containers(nested_container_t regular_value, + // @loc[64]=extern_value_decl @goto-def-decl[64]->extern_value_decl @goto-type-def[64]->extern_nested_container_t_def + extern_nested_container_t extern_value) { + } + + // @diag-error: Cannot use anonymous struct type in argument type + method rejects_inline_struct(struct { int value; } inline_value) { + } + + // @goto-def-decl[33]->typed_pair_argument @goto-type-def[33]->int_alias_def @goto-def-decl[54]->typed_pair_flag @goto-type-def[54]->bool_alias_def + method typed_pair(int_alias argument, bool_alias flag) -> (int, bool) { // @loc[12]=typed_pair_method @loc[33]=typed_pair_argument @loc[54]=typed_pair_flag + return (argument, flag); + } + + method valid_scalar_return() -> (int) { // @loc[12]=valid_scalar_method + return 1; + } + + method typed_text() -> (const char *) { // @loc[12]=typed_text_method + return "nested"; + } + + // @goto-def-decl[34]->accepts_int_arg @goto-type-def[34]->int_alias_def + method accepts_int(int_alias value) { // @loc[12]=accepts_int_method @loc[34]=accepts_int_arg + } + + method nested_call_argument() { + // @diag-error: Cannot pass value of type 'const char *' | related->accepts_int_arg: Argument declared here + accepts_int(typed_text()); // @goto-def-decl[9]->accepts_int_method @goto-def-decl[21]->typed_text_method + } + + // @goto-def-decl[68]->value_arg @goto-type-def[68]->uint8_alias_def + inline method argument_assignment(inline selector, uint8_alias value) { // @loc[46]=selector_arg @loc[68]=value_arg + // @diag-error: Cannot assign to inline method argument 'selector' | related->selector_arg: Inline argument declared here + selector = 1; // @goto-def-decl[9]->selector_arg + // @diag-error: Cannot assign value of type 'const char *' | related->value_arg: declared as 'uint8_alias' here + value = "argument assignment"; // @goto-def-decl[9]->value_arg + } + + method test_method_types() { + // @loc[26]=call_int_decl @goto-def-decl[26]->call_int_decl @goto-type-def[26]->int_alias_def @loc[47]=call_bool_decl @goto-def-decl[47]->call_bool_decl @goto-type-def[47]->bool_alias_def + local (int_alias call_int, bool_alias call_bool) = typed_pair(4, false); + // @loc[25]=assigned_call_int_decl @goto-def-decl[25]->assigned_call_int_decl @goto-type-def[25]->int_alias_def + local int_alias assigned_call_int; + // @loc[26]=assigned_call_bool_decl @goto-def-decl[26]->assigned_call_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias assigned_call_bool; + (assigned_call_int, assigned_call_bool) = typed_pair(4, false); + // @diag-error: returns 2 values but assignment has 1 targets | related->typed_pair_method: Method declared here + assigned_call_int = typed_pair(4, false); + // @diag-error: returns 1 values but assignment has 2 targets | related->valid_scalar_method: Method declared here + (assigned_call_int, assigned_call_bool) = valid_scalar_return(); + // @loc[26]=bad_arg_int_decl @goto-def-decl[26]->bad_arg_int_decl @goto-type-def[26]->int_alias_def @loc[50]=bad_arg_bool_decl @goto-def-decl[50]->bad_arg_bool_decl @goto-type-def[50]->bool_alias_def + local (int_alias bad_arg_int, bool_alias bad_arg_bool) = + // @diag-error: argument 'argument' of method 'typed_pair' | related->typed_pair_argument: Argument declared here + // @diag-error: argument 'flag' of method 'typed_pair' | related->typed_pair_flag: Argument declared here + typed_pair("argument", 4); // @goto-def-decl[13]->typed_pair_method + // @diag-error: expects 2 arguments but call provides 1 | related->typed_pair_method: Method declared here + // @loc[26]=missing_arg_int_decl @goto-def-decl[26]->missing_arg_int_decl @goto-type-def[26]->int_alias_def @loc[54]=missing_arg_bool_decl @goto-def-decl[54]->missing_arg_bool_decl @goto-type-def[54]->bool_alias_def + local (int_alias missing_arg_int, bool_alias missing_arg_bool) = typed_pair(4); + // @diag-error: returns 2 values but declaration binds 1 | related->typed_pair_method: Method declared here + // @loc[25]=short_binding_decl @goto-def-decl[25]->short_binding_decl @goto-type-def[25]->int_alias_def + local int_alias short_binding = typed_pair(4, false); + // @diag-error: bad_bind_bool | related->bad_bind_bool_decl: declared here + // @diag-error: bad_bind_int | related->bad_bind_int_decl: declared here + // @goto-def-decl[27]->bad_bind_bool_decl @goto-type-def[27]->bool_alias_def @goto-def-decl[52]->bad_bind_int_decl @goto-type-def[52]->int_alias_def + local (bool_alias bad_bind_bool, int_alias bad_bind_int) = typed_pair(4, false); // @loc[27]=bad_bind_bool_decl @loc[52]=bad_bind_int_decl + // @goto-def-decl[26]->bad_assigned_bool_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_assigned_call_bool; // @loc[26]=bad_assigned_bool_decl + // @goto-def-decl[25]->bad_assigned_int_decl @goto-type-def[25]->int_alias_def + local int_alias bad_assigned_call_int; // @loc[25]=bad_assigned_int_decl + // @diag-error: bad_assigned_call_int | related->bad_assigned_int_decl: declared here + // @diag-error: bad_assigned_call_bool | related->bad_assigned_bool_decl: declared here + (bad_assigned_call_bool, bad_assigned_call_int) = typed_pair(4, false); + + // @loc[26]=object_arg_int_decl @goto-def-decl[26]->object_arg_int_decl @goto-type-def[26]->int_alias_def @loc[53]=object_arg_bool_decl @goto-def-decl[53]->object_arg_bool_decl @goto-type-def[53]->bool_alias_def + local (int_alias object_arg_int, bool_alias object_arg_bool) = + // @diag-error: argument 'argument' of method 'object_pair' | related->object_pair_argument: Argument declared here + // @diag-error: argument 'flag' of method 'object_pair' | related->object_pair_flag: Argument declared here + // @loc[26]=object_pair_bad_arg_call + typed_object.object_pair("object argument", 4); // @goto-def-decl[26]->object_pair_method + // @loc[26]=object_short_int_decl @goto-def-decl[26]->object_short_int_decl @goto-type-def[26]->int_alias_def @loc[55]=object_short_bool_decl @goto-def-decl[55]->object_short_bool_decl @goto-type-def[55]->bool_alias_def + local (int_alias object_short_int, bool_alias object_short_bool) = + // @diag-error: expects 2 arguments but call provides 1 | related->object_pair_method: Method declared here + // @loc[26]=object_pair_short_call + typed_object.object_pair(4); // @goto-def-decl[26]->object_pair_method + // @goto-def-decl[27]->bad_object_bind_bool_decl @goto-type-def[27]->bool_alias_def @goto-def-decl[59]->bad_object_bind_int_decl @goto-type-def[59]->int_alias_def + local (bool_alias bad_object_bind_bool, int_alias bad_object_bind_int) = // @loc[27]=bad_object_bind_bool_decl @loc[59]=bad_object_bind_int_decl + // @diag-error: bad_object_bind_bool | related->bad_object_bind_bool_decl: declared here + // @diag-error: bad_object_bind_int | related->bad_object_bind_int_decl: declared here + // @loc[26]=object_pair_bad_bind_call + typed_object.object_pair(4, false); // @goto-def-decl[26]->object_pair_method + } +} diff --git a/tests/test_files/type_name_lookup.dml b/tests/test_files/type_name_lookup.dml new file mode 100644 index 00000000..0afdba93 --- /dev/null +++ b/tests/test_files/type_name_lookup.dml @@ -0,0 +1,20 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; + +device type_name_lookup_device; + +// @loc[16]=address_t_def +typedef uint32 address_t; // @goto-ref[16]->address_t_saved_use,address_t_arg_use + +// @loc[7]=address_t_saved_use +// @loc[17]=current_address_decl @goto-def-decl[17]->current_address_decl +saved address_t current_address; // @goto-def-decl[7]->address_t_def @goto-type-def[17]->address_t_def + +method use_address( + // @loc[5]=address_t_arg_use + // @loc[15]=address_decl @goto-def-decl[15]->address_decl + address_t address // @goto-type-def[15]->address_t_def +) { +} diff --git a/tests/test_files/type_narrowing.dml b/tests/test_files/type_narrowing.dml new file mode 100644 index 00000000..487c0c16 --- /dev/null +++ b/tests/test_files/type_narrowing.dml @@ -0,0 +1,69 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_narrowing; + +typedef int int_alias; // @loc[13]=int_alias_def +typedef long long_alias; // @loc[14]=long_alias_def +typedef uint64 uint64_alias; // @loc[16]=uint64_alias_def +typedef uint8 uint8_alias; // @loc[15]=uint8_alias_def + +bank diagnostics { + // @loc[36]=value_decl @goto-def-decl[36]->value_decl @goto-type-def[36]->uint8_alias_def + method takes_uint8(uint8_alias value) { + } + + method returns_uint64() -> (uint64) { + return 256; + } + + // @loc[43]=wide_decl @goto-def-decl[43]->wide_decl @goto-type-def[43]->uint64_alias_def + method truncating_return(uint64_alias wide) -> (uint8) { + return wide; + } + + // @loc[50]=wide_decl_2 @goto-def-decl[50]->wide_decl_2 @goto-type-def[50]->uint64_alias_def + method suppressed_warning_paths(uint64_alias wide) { + takes_uint8(wide); + // @loc[27]=from_call_decl @goto-def-decl[27]->from_call_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias from_call = returns_uint64(); + // @loc[27]=array_decl @goto-def-decl[27]->array_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias array[1] = {wide}; + } + + method narrowing() { + // @loc[28]=wide_decl_3 @goto-def-decl[28]->wide_decl_3 @goto-type-def[28]->uint64_alias_def + local uint64_alias wide = 1; + // @loc[27]=fitting_literal_decl @goto-def-decl[27]->fitting_literal_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias fitting_literal = 255; + // @disabled-diag-warning: truncated_literal | related->truncated_literal_decl: declared as 'uint8' here + // @loc[27]=truncated_literal_decl @goto-def-decl[27]->truncated_literal_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias truncated_literal = 256; // @disabled-location[27]=truncated_literal_decl + // @disabled-diag-warning: narrowed | related->narrowed_decl: declared as 'uint8' here + // @loc[27]=narrowed_decl @goto-def-decl[27]->narrowed_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias narrowed = wide; // @disabled-location[27]=narrowed_decl + // @disabled-diag-warning: platform_narrowed | related->platform_narrowed_decl: declared as 'long' here + // @loc[26]=platform_narrowed_decl @goto-def-decl[26]->platform_narrowed_decl @goto-type-def[26]->long_alias_def + local long_alias platform_narrowed = wide; // @disabled-location[26]=platform_narrowed_decl + // @loc[27]=fitting_assignment_decl @goto-def-decl[27]->fitting_assignment_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias fitting_assignment; + fitting_assignment = 255; + // @loc[27]=narrowed_assignment_decl @goto-def-decl[27]->narrowed_assignment_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias narrowed_assignment; // @disabled-location[27]=narrowed_assignment_decl + // @disabled-diag-warning: narrowed_assignment | related->narrowed_assignment_decl: declared as 'uint8' here + narrowed_assignment = wide; + // @loc[27]=truncated_assignment_decl @goto-def-decl[27]->truncated_assignment_decl @goto-type-def[27]->uint8_alias_def + local uint8_alias truncated_assignment; // @disabled-location[27]=truncated_assignment_decl + // @disabled-diag-warning: truncated_assignment | related->truncated_assignment_decl: declared as 'uint8' here + truncated_assignment = 256; + // @loc[26]=platform_assignment_decl @goto-def-decl[26]->platform_assignment_decl @goto-type-def[26]->long_alias_def + local long_alias platform_assignment; // @disabled-location[26]=platform_assignment_decl + // @disabled-diag-warning: platform_assignment | related->platform_assignment_decl: declared as 'long' here + platform_assignment = wide; + // @goto-def-decl[25]->incompatible_assignment_decl @goto-type-def[25]->int_alias_def + local int_alias incompatible_assignment; // @loc[25]=incompatible_assignment_decl + // @diag-error: incompatible_assignment | related->incompatible_assignment_decl: declared as 'int_alias' here + incompatible_assignment = "123"; + } +} diff --git a/tests/test_files/type_overrides.dml b/tests/test_files/type_overrides.dml new file mode 100644 index 00000000..2e8547df --- /dev/null +++ b/tests/test_files/type_overrides.dml @@ -0,0 +1,34 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_overrides; + +typedef int int_alias; // @loc[13]=int_alias_def +typedef uint64 uint64_alias; // @loc[16]=uint64_alias_def +typedef uint8 uint8_alias; // @loc[15]=uint8_alias_def + +template typed_base { + // @goto-def-decl[29]->base_update_arg @goto-type-def[29]->int_alias_def + method update(int_alias value) default { // @loc[29]=base_update_arg + } + + // @goto-def-decl[39]->base_narrow_arg @goto-type-def[39]->uint64_alias_def + method update_narrow(uint64_alias value) default { // @loc[39]=base_narrow_arg + } +} + +template typed_override is typed_base { + // @diag-error: Mismatching argument type in method override | related->base_update_arg: Corresponding argument declared here + // @loc[32]=value_decl @goto-def-decl[32]->value_decl @goto-type-def[32]->uint8_alias_def + method update(uint8_alias *value) { + } + + // @diag-error: Mismatching argument type in method override | related->base_narrow_arg: Corresponding argument declared here + // @loc[38]=value_decl_2 @goto-def-decl[38]->value_decl_2 @goto-type-def[38]->uint8_alias_def + method update_narrow(uint8_alias value) { + } +} + +bank diagnostics is typed_override { +} diff --git a/tests/test_files/type_returns.dml b/tests/test_files/type_returns.dml new file mode 100644 index 00000000..71d7205e --- /dev/null +++ b/tests/test_files/type_returns.dml @@ -0,0 +1,69 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_returns; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def + +template shared_return_source { + shared method shared_text() -> (const char *) { + return "shared"; + } +} + +template shared_return_calls is shared_return_source { + shared method invalid_shared_forward() -> (int) { // @loc[48]=shared_forward_return + // @diag-error: Cannot return value of type 'const char *' | related->shared_forward_return: Return type declared here + return shared_text(); + } +} + +bank shared_return_diagnostics is shared_return_calls { +} + +bank diagnostics { + // @loc[33]=argument_decl @goto-def-decl[33]->argument_decl @goto-type-def[33]->int_alias_def @loc[54]=flag_decl @goto-def-decl[54]->flag_decl @goto-type-def[54]->bool_alias_def + method typed_pair(int_alias argument, bool_alias flag) -> (int, bool) { + return (argument, flag); + } + + method invalid_return() -> (int, bool) { // @loc[33]=invalid_return_int @loc[38]=invalid_return_bool + // @diag-error: return value of type 'const char *' | related->invalid_return_int: Return type declared here + // @diag-error: return value of type 'uint64' | related->invalid_return_bool: Return type declared here + return ("return", 4); + } + + method valid_scalar_return() -> (int) { + return 1; + } + + method invalid_scalar_return() -> (int) { // @loc[40]=invalid_scalar_return_type + // @diag-error: return value of type 'const char *' | related->invalid_scalar_return_type: Return type declared here + return "scalar return"; + } + + method unresolved_call_return() -> (int) { + return unknown_get(); + } + + method unknown_tuple_return() -> (int) { // @loc[12]=unknown_tuple_method + // @diag-error: declares 1 return values but return statement provides 2 | related->unknown_tuple_method: Method declared here + return (null, 1); + } + + method known_tuple_return() -> (int) { // @loc[12]=known_tuple_method + // @diag-error: declares 1 return values but return statement provides 2 | related->known_tuple_method: Method declared here + return (2, 1); + } + + method short_return() -> (int, bool) { // @loc[12]=short_return_method + // @diag-error: declares 2 return values but return statement provides 1 | related->short_return_method: Method declared here + return 4; + } + + method forwarded_return() -> (int, bool) { + return typed_pair(4, false); + } +} diff --git a/tests/test_files/type_structs.dml b/tests/test_files/type_structs.dml new file mode 100644 index 00000000..20ea0bd4 --- /dev/null +++ b/tests/test_files/type_structs.dml @@ -0,0 +1,77 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_structs; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def + +typedef struct { + int value; +} diagnostic_inner_t; + +extern typedef struct { + transaction_t *prev; + struct { } *_cache; +// @loc[3]=transaction_t_def +} transaction_t; + +typedef struct { + int value; + int *pointer; + int array[2]; + diagnostic_inner_t nested; +// @loc[3]=outer_type_def +} diagnostic_outer_t; // @goto-ref[3]->outer_type_use,outer_pointer_type_use + +bank diagnostics { + // @loc[52]=modified_txn_decl @goto-def-decl[52]->modified_txn_decl @goto-type-def[52]->transaction_t_def + method recursive_struct_pointer(transaction_t *modified_txn) { + // @loc[30]=prev_modified_decl @goto-def-decl[30]->prev_modified_decl @goto-type-def[30]->transaction_t_def + local transaction_t *prev_modified = modified_txn->prev; + } + + method struct_fields( + // @loc[9]=outer_type_use + // @loc[28]=value_decl @goto-def-decl[28]->value_decl @goto-type-def[28]->outer_type_def + diagnostic_outer_t value, // @goto-def-decl[9]->outer_type_def + // @loc[9]=outer_pointer_type_use + // @loc[29]=pointer_decl @goto-def-decl[29]->pointer_decl @goto-type-def[29]->outer_type_def + diagnostic_outer_t *pointer) { // @goto-def-decl[9]->outer_type_def + // @loc[25]=scalar_field_decl @goto-def-decl[25]->scalar_field_decl @goto-type-def[25]->int_alias_def + local int_alias scalar_field = value.value; + // @loc[26]=pointer_field_decl @goto-def-decl[26]->pointer_field_decl @goto-type-def[26]->int_alias_def + local int_alias *pointer_field = value.pointer; + // @loc[26]=array_field_decl @goto-def-decl[26]->array_field_decl @goto-type-def[26]->int_alias_def + local int_alias *array_field = value.array; + // @loc[25]=array_element_decl @goto-def-decl[25]->array_element_decl @goto-type-def[25]->int_alias_def + local int_alias array_element = value.array[0]; + // @loc[25]=nested_field_decl @goto-def-decl[25]->nested_field_decl @goto-type-def[25]->int_alias_def + local int_alias nested_field = value.nested.value; + // @loc[25]=indirect_field_decl @goto-def-decl[25]->indirect_field_decl @goto-type-def[25]->int_alias_def + local int_alias indirect_field = pointer->value; + + // @diag-error: bad_scalar_field | related->bad_scalar_field_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_scalar_field_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_scalar_field = value.value; // @loc[26]=bad_scalar_field_decl + // @diag-error: bad_pointer_field | related->bad_pointer_field_decl: declared as 'int_alias' here + // @goto-def-decl[25]->bad_pointer_field_decl @goto-type-def[25]->int_alias_def + local int_alias bad_pointer_field = value.pointer; // @loc[25]=bad_pointer_field_decl + // @diag-error: bad_array_field | related->bad_array_field_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_array_field_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_array_field = value.array; // @loc[26]=bad_array_field_decl + // @diag-error: bad_array_element | related->bad_array_element_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_array_element_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_array_element = pointer->array[0]; // @loc[26]=bad_array_element_decl + // @diag-error: bad_nested_field | related->bad_nested_field_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_nested_field_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_nested_field = value.nested.value; // @loc[26]=bad_nested_field_decl + // @diag-error: bad_indirect_field | related->bad_indirect_field_decl: declared as 'bool_alias' here + // @goto-def-decl[26]->bad_indirect_field_decl @goto-type-def[26]->bool_alias_def + local bool_alias bad_indirect_field = pointer->value; // @loc[26]=bad_indirect_field_decl + // @diag-error: Struct type has no field named 'missing' + // @loc[25]=missing_field_decl @goto-def-decl[25]->missing_field_decl @goto-type-def[25]->int_alias_def + local int_alias missing_field = value.missing; + } +} diff --git a/tests/test_files/type_switch_scope.dml b/tests/test_files/type_switch_scope.dml new file mode 100644 index 00000000..17dc0a0a --- /dev/null +++ b/tests/test_files/type_switch_scope.dml @@ -0,0 +1,25 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_switch_scope; + +typedef int int_alias; // @loc[13]=int_alias_def + +bank diagnostics { + // @loc[35]=selector_decl @goto-def-decl[35]->selector_decl @goto-type-def[35]->int_alias_def + method switch_scope(int_alias selector) { + switch (selector) { + case 0: + // @goto-def-decl[29]->switched_decl @goto-type-def[29]->int_alias_def + local int_alias switched; // @loc[29]=switched_decl + break; + case 1: + // @diag-error: Cannot assign value of type 'const char *' | related->switched_decl: declared as 'int_alias' here + switched = "switch assignment"; + break; + } + // @diag-error: Cannot assign value of type 'const char *' | related->switched_decl: declared as 'int_alias' here + switched = "post-switch assignment"; + } +} diff --git a/tests/test_files/type_template_types.dml b/tests/test_files/type_template_types.dml new file mode 100644 index 00000000..6a79f0f0 --- /dev/null +++ b/tests/test_files/type_template_types.dml @@ -0,0 +1,26 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_template_types; + +// @loc[10]=template_as_type_def +// @goto-ref[10]->template_as_type_instantiation,template_as_type_type_ref1,template_as_type_type_ref2 +// @goto-impl[10]->template_as_type_instantiation +template template_as_type {} + +// @loc[7]=template_user_def +// @loc[24]=template_as_type_instantiation +// @goto-def-decl[24]->template_as_type_def +// @goto-ref[7]->template_user_cast_ref +group template_user is template_as_type {} + +// @loc[9]=template_as_type_type_ref1 +// @loc[51]=template_user_cast_ref +// @loc[66]=template_as_type_type_ref2 +// @goto-def-decl[9]->template_as_type_def +// @goto-type-def[26]->template_as_type_def +// @goto-def-decl[51]->template_user_def +// @goto-def-decl[66]->template_as_type_def +// @loc[26]=template_user_ref_decl @goto-def-decl[26]->template_user_ref_decl +session template_as_type template_user_ref = cast(template_user, template_as_type); diff --git a/tests/test_files/type_variables.dml b/tests/test_files/type_variables.dml new file mode 100644 index 00000000..68fc8438 --- /dev/null +++ b/tests/test_files/type_variables.dml @@ -0,0 +1,106 @@ +// © 2026 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT + +dml 1.4; +device type_variables; + +typedef bool bool_alias; // @loc[14]=bool_alias_def +typedef int int_alias; // @loc[13]=int_alias_def + +// @loc[16]=register_addr_t_def +typedef uint32 register_addr_t; // @goto-ref[16]->register_addr_t_use_saved,register_addr_t_use_arg,register_addr_t_use_local + +typedef struct { + // @loc[12]=packet_addr_field_def + uint32 addr; // @goto-ref[12]->packet_addr_field_use,packet_addr_field_use_2 + // @loc[12]=packet_size_field_def + uint32 size; // @goto-ref[12]->packet_size_field_use + uint8 data[16]; +// @loc[3]=packet_t_def +} packet_t; // @goto-ref[3]->packet_t_use_arg,packet_t_use_saved + +// @loc[7]=register_addr_t_use_saved +// @loc[23]=current_addr_def +// @goto-def-decl[23]->current_addr_def +saved register_addr_t current_addr; // @goto-def-decl[7]->register_addr_t_def @goto-type-def[23]->register_addr_t_def @goto-ref[23]->current_addr_use_1,current_addr_use_2 + +// @loc[7]=packet_t_use_saved +// @loc[16]=last_packet_def +// @goto-def-decl[16]->last_packet_def +saved packet_t last_packet; // @goto-def-decl[7]->packet_t_def @goto-type-def[16]->packet_t_def @goto-ref[16]->last_packet_use,last_packet_use_2 + +// @loc[8]=process_method_def +method process( // @goto-ref[8]->process_method_use + // @loc[5]=packet_t_use_arg + // @loc[14]=pkt_arg_def + // @goto-def-decl[14]->pkt_arg_def @goto-type-def[14]->packet_t_def + packet_t pkt, // @goto-ref[14]->pkt_arg_use_1,pkt_arg_use_2,pkt_arg_use_3,pkt_arg_use_4 + // @loc[5]=register_addr_t_use_arg + // @loc[21]=base_addr_arg_def + // @goto-def-decl[21]->base_addr_arg_def @goto-type-def[21]->register_addr_t_def + register_addr_t base_addr // @goto-ref[21]->base_addr_arg_use_1,base_addr_arg_use_2 +) { + // @loc[11]=register_addr_t_use_local + // @loc[27]=local_addr_def + // @goto-def-decl[27]->local_addr_def + local register_addr_t local_addr; // @goto-def-decl[11]->register_addr_t_def @goto-type-def[27]->register_addr_t_def @goto-ref[27]->local_addr_use + // @loc[5]=local_addr_use + local_addr = 0; // @goto-def-decl[5]->local_addr_def + + // @loc[5]=current_addr_use_1 + // @loc[20]=base_addr_arg_use_1 + current_addr = base_addr; // @goto-def-decl[5]->current_addr_def @goto-def-decl[20]->base_addr_arg_def + + // @loc[20]=pkt_arg_use_1 + // @loc[24]=packet_addr_field_use + // @loc[31]=base_addr_arg_use_2 + // @loc[5]=current_addr_use_2 + current_addr = pkt.addr + base_addr; // @goto-def-decl[5]->current_addr_def @goto-def-decl[20]->pkt_arg_def @goto-def-decl[24]->packet_addr_field_def @goto-def-decl[31]->base_addr_arg_def + + // @loc[34]=pkt_arg_use_2 + // @loc[38]=packet_addr_field_use_2 + // @loc[44]=pkt_arg_use_3 + // @loc[48]=packet_size_field_use + log info: "addr=%d size=%d", pkt.addr, pkt.size; // @goto-def-decl[34]->pkt_arg_def @goto-def-decl[38]->packet_addr_field_def @goto-def-decl[44]->pkt_arg_def @goto-def-decl[48]->packet_size_field_def + + // @loc[5]=last_packet_use + // @loc[19]=pkt_arg_use_4 + last_packet = pkt; // @goto-def-decl[5]->last_packet_def @goto-def-decl[19]->pkt_arg_def @goto-type-def[5]->packet_t_def @goto-type-def[19]->packet_t_def +} + +method kickoff() { + // @loc[5]=process_method_use + // @loc[13]=last_packet_use_2 + process(last_packet, 0); // @goto-def-decl[5]->process_method_def @goto-def-decl[13]->last_packet_def +} + +method valid_scalar_return() -> (int) { + return 1; +} + +method bad_initializers() { + // @loc[21]=valid_decl @goto-def-decl[21]->valid_decl @goto-type-def[21]->int_alias_def + local int_alias valid = 123; + // @loc[21]=copied_decl @goto-def-decl[21]->copied_decl @goto-type-def[21]->int_alias_def + local int_alias copied = valid; + // @diag-error: bad_typeof_local | related->bad_typeof_local_decl: declared as 'int_alias' here + // NOTE: goto-type-def through typeof(...) is not supported yet; ideally + // this would find int_alias_def + local typeof(valid) bad_typeof_local = "typeof local"; // @loc[25]=bad_typeof_local_decl @goto-def-decl[25]->bad_typeof_local_decl @goto-type-def[25]-> + // @diag-error: bad_typeof_literal | related->bad_typeof_literal_decl: declared as 'bool' here + local typeof(false) bad_typeof_literal = 1; // @loc[25]=bad_typeof_literal_decl @goto-def-decl[25]->bad_typeof_literal_decl @goto-type-def[25]-> + // @diag-error: bad_typeof_call | related->bad_typeof_call_decl: declared as 'int' here + local typeof(valid_scalar_return()) bad_typeof_call = "typeof call"; // @loc[41]=bad_typeof_call_decl @goto-def-decl[41]->bad_typeof_call_decl @goto-type-def[41]-> + local typeof(unknown_type_source) unresolved_typeof = "unknown"; // @loc[39]=unresolved_typeof_decl @goto-def-decl[39]->unresolved_typeof_decl @goto-type-def[39]-> + // @diag-error: bad_typeof_cast | related->bad_typeof_cast_decl: declared as 'bool_alias' here + // @loc[22]=bad_typeof_cast_decl_2 @goto-def-decl[22]->bad_typeof_cast_decl_2 @goto-type-def[22]->bool_alias_def + local bool_alias bad_typeof_cast = cast(1, typeof(valid)); // @loc[26]=bad_typeof_cast_decl + // @diag-error: Cannot initialize local variable 'test' | related->test_decl: declared as 'int_alias' here + // @goto-def-decl[21]->test_decl @goto-type-def[21]->int_alias_def + local int_alias test = "123"; // @loc[21]=test_decl + // @diag-error: boolean_test | related->boolean_test_decl: declared as 'int_alias' here + // @loc[21]=boolean_test_decl_2 @goto-def-decl[21]->boolean_test_decl_2 @goto-type-def[21]->int_alias_def + local int_alias boolean_test = false; // @loc[25]=boolean_test_decl + // @loc[21]=object_test_decl @goto-def-decl[21]->object_test_decl @goto-type-def[21]->int_alias_def + local int_alias object_test = some_random_object; +}