From 1b4172769c6768eadf2d09e90ce8d63f1c3cba04 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 31 Oct 2026 01:21:38 +0900 Subject: [PATCH] refactor(sv): resolve handle classes from expression types Add expression_types::type_of, the semantic type of a native expression judged before it is bound: variables from their shapes, members of unpacked structures and container elements, elements selected from arrays and containers, properties of classes, `super`, `process::self()` and the results of functions and methods. objects::native_class is now the class of that type, with the same resolution as before, so its callers keep their behavior while later checks adopt the expression types. Change-Id: Ic2efb74b6dd94f6b654953e7aa22ffb17b4bf7bd --- crates/celox-sv-analyzer/src/ast.rs | 1 + .../src/ast/expression_binding.rs | 2 +- .../src/ast/expression_binding/objects.rs | 136 +++++----------- .../src/ast/expression_types.rs | 152 ++++++++++++++++++ .../src/ast/semantic_types.rs | 2 +- 5 files changed, 195 insertions(+), 98 deletions(-) create mode 100644 crates/celox-sv-analyzer/src/ast/expression_types.rs diff --git a/crates/celox-sv-analyzer/src/ast.rs b/crates/celox-sv-analyzer/src/ast.rs index 72556af556b..183da81ab40 100644 --- a/crates/celox-sv-analyzer/src/ast.rs +++ b/crates/celox-sv-analyzer/src/ast.rs @@ -223,6 +223,7 @@ mod dimensions; mod dpi; mod enums; mod expression_binding; +mod expression_types; mod expressions; mod ff_process; mod functions; diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding.rs b/crates/celox-sv-analyzer/src/ast/expression_binding.rs index 63805b64113..3d4acc5d059 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding.rs @@ -7,7 +7,7 @@ mod direct; mod enums; mod format_patterns; mod lvalues; -mod objects; +pub(super) mod objects; mod references; #[cfg(test)] mod selected; diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs index e3228a4f890..ac19baf73c3 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs @@ -37,98 +37,8 @@ pub(in crate::ast) fn native_class( native: &Declarations, dimensions: &PackedDimensions, ) -> Option { - let id = ungroup(id, native); - if let ExprKind::Call { expression, .. } = &native.expression(id).kind - && let ExprKind::Member { - expression: base, - separator, - name, - } = &native.expression(*expression).kind - && separator == "::" - && name.spelling == "self" - && super::super::processes::scope(tree, session, *base, native, dimensions) - { - return Some(super::super::processes::CLASS.to_owned()); - } - // The result of a function or method that returns a class handle, whose - // members the call may select (IEEE 1800-2023 8.4). - if let ExprKind::Call { expression, .. } = &native.expression(id).kind { - let name = match method(tree, session, *expression, native, dimensions) { - Ok(Some((name, ..))) => name, - Ok(None) => super::direct::callable_name(tree, *expression, native)?, - Err(_) => return None, - }; - return dimensions.function_return_types.get(&name)?.class.clone(); - } - // A class handle member of an unpacked structure variable. - if let Some(name) = unpacked_structs::native_member(ungroup(id, native), native, dimensions) { - return dimensions.get(&name)?.class.clone(); - } - // A class handle member of a container element of unpacked structure type. - if let Ok(Some((ty, _))) = containers::element_member(id, native, tree, session, dimensions) { - return ty.class().map(str::to_owned); - } - let expression = native.expression(ungroup(id, native)); - if let ExprKind::Select { - expression, - dimension, - } = &expression.kind - && matches!(dimension.kind, celox_sv_syntax::DimensionKind::Index(_)) - { - if let Some(ty) = containers::native_type(*expression, native, tree, session, dimensions) - .ok() - .flatten() - { - return ty.element.class().map(str::to_owned); - } - let ExprKind::Name(name) = &native.expression(ungroup(*expression, native)).kind else { - return None; - }; - let ty = dimensions.get(&instances::normalize_identifier(&name.spelling))?; - return ty - .container - .as_ref() - .and_then(|ty| ty.element.class().map(str::to_owned)) - .or_else(|| ty.class.clone().filter(|_| !ty.unpacked.is_empty())); - } - if let ExprKind::Name(name) = &expression.kind { - let name = instances::normalize_identifier(&name.spelling); - if let Some(ty) = dimensions.get(&name) { - return ty.class.clone(); - } - if name == "super" { - return super_class(tree, session, dimensions); - } - } - // The receiver of `h.p` is resolved once for both kinds of property, so - // a chain `a.b.c` takes one walk. - if let ExprKind::Member { - expression: receiver, - separator, - name, - } = &expression.kind - && separator == "." - // Otherwise a member of a property of unpacked structure type, found - // as a property below. - && let Some(owner) = native_class(tree, session, *receiver, native, dimensions) - { - let member = instances::normalize_identifier(&name.spelling); - let receiver = (!is_super(*receiver, native)).then_some(*receiver); - if let Some((property, _)) = - property_in(tree, session, dimensions, &owner, &member, receiver).ok()? - { - return property.r#type.class().map(str::to_owned); - } - return class(tree, session, dimensions, &owner) - .ok()? - .static_property(&member) - .and_then(|property| property.r#type.class().map(str::to_owned)); - } - if let Some(class) = static_property_class(tree, session, id, native, dimensions).ok()? { - return Some(class); - } - let (property, _) = property(tree, session, id, native, dimensions).ok()??; - property.r#type.class().map(str::to_owned) + let ty = expression_types::type_of(tree, session, id, native, dimensions)?; + expression_types::class_name(&session.types, ty) } /// Access depends on the lexical class, not the class of the receiver (8.18). @@ -173,10 +83,10 @@ pub(in crate::ast) fn scope_class(dimensions: &PackedDimensions) -> Option, - session: &mut Session, dimensions: &PackedDimensions, + session: &mut Session, ) -> Option { dimensions.get(THIS)?; class(tree, session, dimensions, &scope_class(dimensions)?) @@ -187,7 +97,7 @@ fn super_class( /// Whether native expression `id` is `super`, which passes the enclosing /// method's `this`. -fn is_super(id: ExprId, native: &Declarations) -> bool { +pub(in crate::ast) fn is_super(id: ExprId, native: &Declarations) -> bool { matches!(&native.expression(ungroup(id, native)).kind, ExprKind::Name(name) if name.spelling == "super") } @@ -413,6 +323,19 @@ fn static_property_class( native: &Declarations, dimensions: &PackedDimensions, ) -> Converted> { + Ok(static_property_type(tree, id, native, dimensions, session)? + .and_then(|ty| ty.class().map(str::to_owned))) +} + +/// The type of the static property that native expression `id` names, +/// `h.p` or `C::p`. +pub(in crate::ast) fn static_property_type( + tree: &AnalysisContext<'_>, + id: ExprId, + native: &Declarations, + dimensions: &PackedDimensions, + session: &mut Session, +) -> Converted> { let ExprKind::Member { expression, separator, @@ -431,7 +354,28 @@ fn static_property_class( }; Ok(class(tree, session, dimensions, &class_name)? .static_property(&instances::normalize_identifier(&name.spelling)) - .and_then(|property| property.r#type.class().map(str::to_owned))) + .map(|property| property.r#type.clone())) +} + +/// The type of property `member` of class `owner`, an instance property +/// read through `receiver` or a static one. +pub(in crate::ast) fn member_type( + tree: &AnalysisContext<'_>, + dimensions: &PackedDimensions, + owner: &str, + member: &str, + receiver: Option, + session: &mut Session, +) -> Option { + if let Some((property, _)) = + property_in(tree, session, dimensions, owner, member, receiver).ok()? + { + return Some(property.r#type); + } + class(tree, session, dimensions, owner) + .ok()? + .static_property(member) + .map(|property| property.r#type.clone()) } /// The property that native expression `id` names, `h.p` or an unqualified diff --git a/crates/celox-sv-analyzer/src/ast/expression_types.rs b/crates/celox-sv-analyzer/src/ast/expression_types.rs new file mode 100644 index 00000000000..d5aec1467ff --- /dev/null +++ b/crates/celox-sv-analyzer/src/ast/expression_types.rs @@ -0,0 +1,152 @@ +//! The self-determined types of native expressions, judged before they are +//! bound, as semantic types. +//! +//! Each kind of operand is resolved from what declares it: a variable from +//! its shape, a property from its class, a call from the type its function +//! returns, an element from the array it selects from. An operand that this +//! does not resolve yet has no type, and checks that need one skip it. + +use super::*; +use celox_sv_syntax::{Declarations, DimensionKind, ExprId, ExprKind}; +use expression_binding::{objects, ungroup}; +use semantic_types::{Decl, TyData, TyId}; + +/// The type of native expression `id`. +pub(super) fn type_of( + tree: &AnalysisContext<'_>, + session: &mut Session, + id: ExprId, + native: &Declarations, + dims: &PackedDimensions, +) -> Option { + let of_type = |session: &mut Session, ty: &Type| { + semantic_types::of_type(&mut session.types, &tree.functions, ty, &dims.const_env) + }; + let of_shape = |session: &mut Session, shape: &VariableDimensions| { + semantic_types::of_shape(&mut session.types, &tree.functions, shape, &dims.const_env) + }; + let class = |session: &mut Session, name: &str| { + session + .types + .intern(TyData::Class(Decl::Class(name.into()))) + }; + let id = ungroup(id, native); + if let ExprKind::Call { expression, .. } = &native.expression(id).kind { + // `process::self()` (9.7). + if let ExprKind::Member { + expression: base, + separator, + name, + } = &native.expression(*expression).kind + && separator == "::" + && name.spelling == "self" + && processes::scope(tree, session, *base, native, dims) + { + return Some(class(session, processes::CLASS)); + } + // What the function or method returns. + let name = match objects::method(tree, session, *expression, native, dims) { + Ok(Some((name, ..))) => name, + Ok(None) => expression_binding::callable_name(tree, *expression, native)?, + Err(_) => return None, + }; + let returns = dims.function_return_types.get(&name)?; + return match &returns.class { + Some(name) => Some(class(session, name)), + None if returns.string => Some(session.types.intern(TyData::String)), + None => None, + }; + } + // A member of an unpacked structure variable. + if let Some(name) = unpacked_structs::native_member(id, native, dims) { + return of_shape(session, dims.get(&name)?); + } + // A member of a container element of unpacked structure type. + if let Ok(Some((ty, _))) = containers::element_member(id, native, tree, session, dims) { + return of_type(session, &ty); + } + match &native.expression(id).kind { + ExprKind::Select { + expression, + dimension, + } if matches!(dimension.kind, DimensionKind::Index(_)) => { + if let Some(container) = + containers::native_type(*expression, native, tree, session, dims) + .ok() + .flatten() + { + return of_type(session, &container.element); + } + let ExprKind::Name(name) = &native.expression(ungroup(*expression, native)).kind else { + return None; + }; + let shape = dims.get(&instances::normalize_identifier(&name.spelling))?; + if let Some(container) = &shape.container { + return of_type(session, &container.element); + } + let array = of_shape(session, shape)?; + match session.types.data(array) { + TyData::Array { element, .. } => Some(*element), + _ => None, + } + } + ExprKind::Name(name) => { + let name = instances::normalize_identifier(&name.spelling); + if let Some(shape) = dims.get(&name) { + return of_shape(session, shape); + } + if name == "super" { + let base = objects::super_class(tree, dims, session)?; + return Some(class(session, &base)); + } + property_type(tree, session, id, native, dims) + } + // The receiver of `h.p` is resolved once for both kinds of property, + // so a chain `a.b.c` takes one walk. + ExprKind::Member { + expression: receiver, + separator, + name, + } if separator == "." + // Otherwise a member of a property of unpacked structure type, + // found as a property below. + && let Some(owner) = type_of(tree, session, *receiver, native, dims) + .and_then(|receiver| class_name(&session.types, receiver)) => + { + let member = instances::normalize_identifier(&name.spelling); + let receiver = (!objects::is_super(*receiver, native)).then_some(*receiver); + let ty = objects::member_type(tree, dims, &owner, &member, receiver, session)?; + of_type(session, &ty) + } + _ => property_type(tree, session, id, native, dims), + } +} + +/// The type of the property that native expression `id` names: a static +/// property `h.p` or `C::p`, or an instance property. +fn property_type( + tree: &AnalysisContext<'_>, + session: &mut Session, + id: ExprId, + native: &Declarations, + dims: &PackedDimensions, +) -> Option { + let ty = match objects::static_property_type(tree, id, native, dims, session).ok()? { + Some(ty) => ty, + None => { + objects::property(tree, session, id, native, dims) + .ok()?? + .0 + .r#type + } + }; + semantic_types::of_type(&mut session.types, &tree.functions, &ty, &dims.const_env) +} + +/// The class of a handle of type `id`. +pub(super) fn class_name(table: &semantic_types::TyTable, id: TyId) -> Option { + match table.data(id) { + TyData::Class(Decl::Class(name)) => Some(name.to_string()), + _ => None, + } +} diff --git a/crates/celox-sv-analyzer/src/ast/semantic_types.rs b/crates/celox-sv-analyzer/src/ast/semantic_types.rs index 95f64014b47..de71fc72147 100644 --- a/crates/celox-sv-analyzer/src/ast/semantic_types.rs +++ b/crates/celox-sv-analyzer/src/ast/semantic_types.rs @@ -17,7 +17,7 @@ mod spelling; #[cfg(test)] mod tests; -pub(super) use conversion::of_shape; +pub(super) use conversion::{of_shape, of_type}; /// An interned semantic type of a [`TyTable`]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]