| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Expression footprint and sequencing checks (`Ssenf::check_full_expr`). | |
| use super::*; | |
| mod value; | |
| impl<'a> Ssenf<'a> { | |
| pub fn new(escaped: &'a HashSet<u32>) -> Self { | |
| Ssenf { escaped, ref_fields: None } | |
| } | |
| pub fn with_ref_fields(mut self, ref_fields: &'a HashMap<String, HashSet<u32>>) -> Self { | |
| self.ref_fields = Some(ref_fields); | |
| self | |
| } | |
| fn is_ref_field(&self, record_id: &str, field_idx: u32) -> bool { | |
| self.ref_fields.and_then(|m| m.get(record_id)).is_some_and(|fs| fs.contains(&field_idx)) | |
| } | |
| /// Checks one full-expression, returning its footprint. | |
| pub fn check_full_expr(&self, e: &RawExpr) -> Result<Footprint> { | |
| self.value(e) | |
| } | |
| fn read(&self, fp: &mut Footprint, loc: Loc) { | |
| match loc { | |
| Loc::Var(v) => { | |
| fp.reads.insert(v); | |
| if self.escaped.contains(&v) { | |
| fp.mem_reads = true; | |
| } | |
| } | |
| Loc::Mem => fp.mem_reads = true, | |
| } | |
| } | |
| fn write(&self, fp: &mut Footprint, loc: Loc) { | |
| match loc { | |
| Loc::Var(v) => { | |
| fp.writes.insert(v); | |
| if self.escaped.contains(&v) { | |
| fp.mem_writes += 1; | |
| } | |
| } | |
| Loc::Mem => fp.mem_writes += 1, | |
| } | |
| } | |
| /// The store to `loc` must not coincide with a write performed by an | |
| /// operand (C11 6.5.16p3: the store is sequenced only after the operands' | |
| /// value computations). | |
| fn check_store(&self, what: &str, loc: Loc, operands: &Footprint) -> Result<()> { | |
| match loc { | |
| Loc::Var(v) => { | |
| if operands.writes.contains(&v) { | |
| return reject( | |
| what, | |
| format!( | |
| "the store to variable #{v} is unsequenced with another write of it" | |
| ), | |
| ); | |
| } | |
| if self.escaped.contains(&v) && operands.mem_writes > 0 { | |
| return reject( | |
| what, | |
| format!("the store to address-taken variable #{v} is unsequenced with a memory write"), | |
| ); | |
| } | |
| } | |
| Loc::Mem => { | |
| if operands.mem_writes > 0 { | |
| return reject( | |
| what, | |
| "a memory store is unsequenced with another memory write".to_owned(), | |
| ); | |
| } | |
| if operands.writes.iter().any(|v| self.escaped.contains(v)) { | |
| return reject( | |
| what, | |
| "a memory store is unsequenced with a write to an address-taken local" | |
| .to_owned(), | |
| ); | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } | |
| /// Footprint of computing the address designated by an lvalue. | |
| fn lvalue(&self, e: &RawExpr) -> Result<(Footprint, Loc)> { | |
| match &e.kind { | |
| // A reference designates some object in memory. | |
| RawExprKind::VarRef { is_ref_binding: true, .. } => { | |
| Ok((Footprint::default(), Loc::Mem)) | |
| } | |
| RawExprKind::VarRef { var_id, .. } => Ok((Footprint::default(), Loc::Var(*var_id))), | |
| RawExprKind::Cast { | |
| kind: | |
| CastKind::Noop | CastKind::DerivedToBase { .. } | CastKind::UserDefinedConversion, | |
| sub, | |
| } if e.is_lvalue => self.lvalue(sub), | |
| RawExprKind::MaterializeTemporary { sub, .. } => Ok((self.value(sub)?, Loc::Mem)), | |
| // A fresh unnamed object, conservatively treated as memory. | |
| RawExprKind::CompoundLiteral { init, .. } => Ok((self.value(init)?, Loc::Mem)), | |
| RawExprKind::GlobalRef { .. } => Ok((Footprint::default(), Loc::Mem)), | |
| RawExprKind::Paren { sub } | RawExprKind::ExprWithCleanups { sub } => self.lvalue(sub), | |
| RawExprKind::Unary { op: UnaryOp::Deref, sub } => Ok((self.value(sub)?, Loc::Mem)), | |
| RawExprKind::ArraySubscript { base, idx } => { | |
| let (b, loc) = match &strip_parens(base).kind { | |
| // `local_array[i]` designates (part of) the local itself. | |
| RawExprKind::Cast { kind: CastKind::ArrayToPointerDecay, sub } => { | |
| let (fp, loc) = self.lvalue(sub)?; | |
| ( | |
| fp, | |
| match loc { | |
| Loc::Var(v) if !self.escaped.contains(&v) => Loc::Var(v), | |
| _ => Loc::Mem, | |
| }, | |
| ) | |
| } | |
| _ => (self.value(base)?, Loc::Mem), | |
| }; | |
| let i = self.value(idx)?; | |
| check_siblings("array subscript", &[b.clone(), i.clone()])?; | |
| Ok((b.union(i), loc)) | |
| } | |
| RawExprKind::Member { base, is_arrow, record_id, field_idx, .. } => { | |
| if self.is_ref_field(record_id, *field_idx) { | |
| if *is_arrow { | |
| let mut fp = self.value(base)?; | |
| fp.mem_reads = true; | |
| Ok((fp, Loc::Mem)) | |
| } else { | |
| let (mut fp, loc) = self.lvalue(base)?; | |
| self.read(&mut fp, loc); | |
| Ok((fp, Loc::Mem)) | |
| } | |
| } else if *is_arrow { | |
| Ok((self.value(base)?, Loc::Mem)) | |
| } else { | |
| self.lvalue(base) | |
| } | |
| } | |
| // C++: comma lvalue evaluates `lhs` and designates `rhs`. | |
| RawExprKind::Binary { op: BinaryOp::Comma, lhs, rhs, .. } if e.is_lvalue => { | |
| let l = self.value(lhs)?; | |
| let (r, loc) = self.lvalue(rhs)?; | |
| Ok((l.union(r), loc)) | |
| } | |
| // C++: assignment and prefix `++`/`--` are lvalues designating | |
| // their target object after the store. | |
| RawExprKind::Binary { | |
| op: BinaryOp::Assign | BinaryOp::CompoundAssign(_), lhs, .. | |
| } if e.is_lvalue => { | |
| let fp = self.value(e)?; | |
| Ok((fp, self.lvalue(lhs)?.1)) | |
| } | |
| RawExprKind::Unary { op: UnaryOp::PreInc | UnaryOp::PreDec, sub } if e.is_lvalue => { | |
| let fp = self.value(e)?; | |
| Ok((fp, self.lvalue(sub)?.1)) | |
| } | |
| // Any other lvalue form is rejected later by lowering; treat it as | |
| // an opaque memory location here. | |
| _ => Ok((self.value(e)?, Loc::Mem)), | |
| } | |
| } | |
| } |