diff --git a/crates/celox-sv-syntax/src/nodes.rs b/crates/celox-sv-syntax/src/nodes.rs index 7d4a6be5969..219e8a8fea3 100644 --- a/crates/celox-sv-syntax/src/nodes.rs +++ b/crates/celox-sv-syntax/src/nodes.rs @@ -960,6 +960,14 @@ pub struct VariableDeclaration { pub tokens: TokenRange, } +/// A named constraint block. Unsupported constraint forms retain their name +/// but no executable expressions, so randomization cannot silently ignore them. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ClassConstraint { + pub name: Name, + pub expressions: Option>, +} + /// A class declaration (IEEE 1800-2023 8.3). Its body also remains an /// unconverted body item covering the same tokens. #[derive(Clone, Debug, Eq, PartialEq)] @@ -976,6 +984,7 @@ pub struct ClassDeclaration { pub interfaces: Vec, /// Whether the class declares constraints, including prototypes. pub has_constraints: bool, + pub constraints: Vec, /// The parameter port list `#(...)`, after which the parameters the /// body declares are local (8.25). pub parameter_ports: Option, diff --git a/crates/celox-sv-syntax/src/parser/classes.rs b/crates/celox-sv-syntax/src/parser/classes.rs index bed49079811..637f457812e 100644 --- a/crates/celox-sv-syntax/src/parser/classes.rs +++ b/crates/celox-sv-syntax/src/parser/classes.rs @@ -223,6 +223,7 @@ impl Parser<'_> { base, interfaces, has_constraints: false, + constraints: Vec::new(), parameter_ports, parameters: Vec::new(), type_aliases: Vec::new(), diff --git a/crates/celox-sv-syntax/src/parser/constraints.rs b/crates/celox-sv-syntax/src/parser/constraints.rs index 3dcdfc0bbc4..2469d9465da 100644 --- a/crates/celox-sv-syntax/src/parser/constraints.rs +++ b/crates/celox-sv-syntax/src/parser/constraints.rs @@ -1,5 +1,5 @@ //! Class constraint syntax validation. Constraints retain their opaque class -//! owner; no randomization or solver semantics are introduced here. +//! owner. Simple expression blocks also retain their expression nodes. use super::*; enum Job { @@ -32,10 +32,26 @@ impl Parser<'_> { class.has_constraints = true; } let prototype = self.eat("extern") || self.eat("pure"); - self.eat("static"); + let is_static = self.eat("static"); self.expect("constraint")?; - self.name()?; + let name = self.name()?; + if self.class_stack.last().is_some_and(|class| { + class + .constraints + .iter() + .any(|block| block.name.spelling == name.spelling) + }) { + return Err(self.error("duplicate constraint block")); + } + let mut expressions = Vec::new(); + let mut supported = !is_static; if self.eat(";") { + if let Some(class) = self.class_stack.last_mut() { + class.constraints.push(ClassConstraint { + name, + expressions: None, + }); + } return Ok(()); } if prototype { @@ -52,6 +68,7 @@ impl Parser<'_> { } jobs.push(Job::Block(solve)); if solve && self.eat("solve") { + supported = false; self.constraint_primary_list()?; self.expect("before")?; self.constraint_primary_list()?; @@ -74,32 +91,41 @@ impl Parser<'_> { } Job::Expression => { if self.eat("if") { + supported = false; self.expect("(")?; self.expression(0)?; self.expect(")")?; jobs.push(Job::Else); jobs.push(Job::Set); } else if self.eat("foreach") { + supported = false; self.foreach_header()?; jobs.push(Job::Set); } else if self.eat("disable") { + supported = false; self.expect("soft")?; self.constraint_primary()?; self.expect(";")?; } else if self.eat("unique") { + supported = false; self.constraint_ranges(false)?; self.expect(";")?; } else { let soft = self.eat("soft"); if soft { + supported = false; self.expression(0)?; } else { - self.constraint_condition()?; + let (expression, simple) = self.constraint_condition()?; + expressions.push(expression); + supported &= simple; } if !soft && self.eat("->") { + supported = false; jobs.push(Job::Set); } else { if self.eat("dist") { + supported = false; self.constraint_ranges(true)?; } self.expect(";")?; @@ -108,17 +134,25 @@ impl Parser<'_> { } } } + if let Some(class) = self.class_stack.last_mut() { + class.constraints.push(ClassConstraint { + name, + expressions: supported.then_some(expressions), + }); + } Ok(()) } - fn constraint_condition(&mut self) -> Result<()> { + fn constraint_condition(&mut self) -> Result<(ExprId, bool)> { // Keep the constraint-set arrow for the outer continuation. Equivalence // shares its expression precedence, so consume it explicitly here. - self.expression(2)?; + let expression = self.expression(2)?; + let mut simple = true; while self.eat("<->") { + simple = false; self.expression(2)?; } - Ok(()) + Ok((expression, simple)) } fn constraint_ranges(&mut self, distribution: bool) -> Result<()> { diff --git a/crates/celox-sv-syntax/src/parser/tests.rs b/crates/celox-sv-syntax/src/parser/tests.rs index c6b29330e73..53d0d1c822a 100644 --- a/crates/celox-sv-syntax/src/parser/tests.rs +++ b/crates/celox-sv-syntax/src/parser/tests.rs @@ -3043,3 +3043,14 @@ fn malformed_array_method_with_is_rejected() { ); } } + +#[test] +fn constraints_retain_simple_expressions_and_mark_other_forms() { + let tree = parse( + "class C; rand int x; constraint a { x > 0; x < 3; } constraint b { if (x) x == 1; } endclass", + ); + let blocks = &tree.classes[0].constraints; + assert_eq!(blocks[0].name.spelling, "a"); + assert_eq!(blocks[0].expressions.as_ref().unwrap().len(), 2); + assert!(blocks[1].expressions.is_none()); +}