Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ mod dimensions;
mod dpi;
mod enums;
mod expression_binding;
mod expression_types;
mod expressions;
mod ff_process;
mod functions;
Expand Down
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/expression_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
136 changes: 40 additions & 96 deletions crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,98 +37,8 @@ pub(in crate::ast) fn native_class(
native: &Declarations,
dimensions: &PackedDimensions,
) -> Option<String> {
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).
Expand Down Expand Up @@ -173,10 +83,10 @@ pub(in crate::ast) fn scope_class(dimensions: &PackedDimensions) -> Option<Strin

/// The base of the scope class, which `super` refers to inside a method that
/// has an object (IEEE 1800-2023 8.15).
fn super_class(
pub(in crate::ast) fn super_class(
tree: &AnalysisContext<'_>,
session: &mut Session,
dimensions: &PackedDimensions,
session: &mut Session,
) -> Option<String> {
dimensions.get(THIS)?;
class(tree, session, dimensions, &scope_class(dimensions)?)
Expand All @@ -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")
}
Expand Down Expand Up @@ -413,6 +323,19 @@ fn static_property_class(
native: &Declarations,
dimensions: &PackedDimensions,
) -> Converted<Option<String>> {
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<Option<Type>> {
let ExprKind::Member {
expression,
separator,
Expand All @@ -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<ExprId>,
session: &mut Session,
) -> Option<Type> {
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
Expand Down
152 changes: 152 additions & 0 deletions crates/celox-sv-analyzer/src/ast/expression_types.rs
Original file line number Diff line number Diff line change
@@ -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<TyId> {
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<TyId> {
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<String> {
match table.data(id) {
TyData::Class(Decl::Class(name)) => Some(name.to_string()),
_ => None,
}
}
2 changes: 1 addition & 1 deletion crates/celox-sv-analyzer/src/ast/semantic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Loading