void update_all_code_references_in_value(caValue* value, Branch* oldBranch, Branch* newBranch) { for (ValueIterator it(value); it.unfinished(); it.advance()) { caValue* val = *it; if (is_ref(val)) { set_term_ref(val, translate_term_across_branches(as_term_ref(val), oldBranch, newBranch)); } else if (is_branch(val)) { // If this is just a reference to 'oldBranch' then simply update it to 'newBranch'. if (as_branch(val) == oldBranch) set_branch(val, newBranch); // Noop on null branch. if (as_branch(val) == NULL) continue; // Noop if branch has no owner. Term* oldTerm = as_branch(val)->owningTerm; if (oldTerm == NULL) continue; Term* newTerm = translate_term_across_branches(oldTerm, oldBranch, newBranch); if (newTerm == NULL) { set_branch(val, NULL); continue; } set_branch(val, newTerm->nestedContents); } } }
void update_all_code_references(Branch* target, Branch* oldBranch, Branch* newBranch) { ca_assert(target != oldBranch); ca_assert(target != newBranch); // Store a cache of lookups that we've made in this call. TermMap cache; for (BranchIterator it(target); it.unfinished(); it.advance()) { Term* term = *it; // Iterate through each "dependency", which includes the function & inputs. for (int i=0; i < term->numDependencies(); i++) { Term* ref = term->dependency(i); Term* newRef = NULL; if (cache.contains(ref)) { newRef = cache[ref]; } else { // Lookup and save result in cache newRef = translate_term_across_branches(ref, oldBranch, newBranch); cache[ref] = newRef; } // Possibly rebind if (newRef != ref) term->setDependency(i, newRef); } } }