/// <summary> /// Clones the whole subtree starting at this AST node. /// </summary> /// <remarks>Annotations are copied over to the new nodes; and any annotations implementing ICloneable will be cloned.</remarks> public AstNode Clone() { AstNode copy = (AstNode)MemberwiseClone(); // First, reset the shallow pointer copies copy.parent = null; copy.firstChild = null; copy.lastChild = null; copy.prevSibling = null; copy.nextSibling = null; copy.flags &= ~frozenBit; // unfreeze the copy // Then perform a deep copy: for (AstNode cur = firstChild; cur != null; cur = cur.nextSibling) { copy.AddChildUnsafe(cur.Clone(), cur.Role); } // Finally, clone the annotation, if necessary copy.CloneAnnotations(); return(copy); }