internal static IEnumerable <Expression> AllSubExpressions(this Expression expr, bool wrapOld, bool strict, bool inlineLets = false) { var isOld = expr is OldExpr ? new HashSet <OldExpr>() { expr as OldExpr } : null; if (inlineLets && expr is LetExpr && ((LetExpr)expr).IsInlineable()) { var le = (LetExpr)expr; foreach (var subexpr in AllSubExpressions(Translator.InlineLet(le), wrapOld, strict, inlineLets)) { yield return(subexpr); } // If strict is false, then the recursive call will already yield a copy of (the inlined version) of expr, // so there's no need to yield expr itself below. yield break; } foreach (var subexpr in expr.SubExpressions) { foreach (var r_subexpr in AllSubExpressions(subexpr, wrapOld, false, inlineLets)) { foreach (var e in TriggerUtils.MaybeWrapInOld(r_subexpr, isOld)) { yield return(e); } } } if (expr is StmtExpr) { foreach (var r_subexpr in AllSubExpressions(((StmtExpr)expr).S, wrapOld, false, inlineLets)) { foreach (var e in TriggerUtils.MaybeWrapInOld(r_subexpr, isOld)) { yield return(e); } } } if (!strict) { yield return(expr); } }
private TriggerAnnotation AnnotatePotentialCandidate(Expression expr) { bool expr_is_killer = false; HashSet <OldExpr> oldExprSet; if (cache.exprsInOldContext.TryGetValue(expr, out oldExprSet)) { // oldExpr has been set to the value found } else { oldExprSet = null; } var new_exprs = TriggerUtils.MaybeWrapInOld(TriggerUtils.PrepareExprForInclusionInTrigger(expr, out expr_is_killer), oldExprSet); // We expect there to be at least one "new_exprs". // We also expect that the computation of new_term.Variables, collected_terms, and children_contain_killers will be the // same for each of the "new_exprs". // Therefore, we use the values of these variables from the last iteration in the expression that is ultimately returned. TriggerTerm new_term = null; List <TriggerTerm> collected_terms = null; var children_contain_killers = false; foreach (var new_expr in new_exprs) { new_term = new TriggerTerm { Expr = new_expr, OriginalExpr = expr, Variables = CollectVariables(expr) }; collected_terms = CollectExportedCandidates(expr); children_contain_killers = CollectIsKiller(expr); if (!children_contain_killers) { // Add only if the children are not killers; the head has been cleaned up into non-killer form collected_terms.Add(new_term); } } Contract.Assert(new_term != null); // this checks our assumption that "new_exprs" contains at least one value. // This new node is a killer if its children were killers, or if it's non-cleaned-up head is a killer return(new TriggerAnnotation(children_contain_killers || expr_is_killer, new_term.Variables, collected_terms)); }