[code-reflection] RFR: CodeElement ancestor functionality

Paul Sandoz psandoz at openjdk.org
Mon Jul 7 20:32:07 UTC 2025


Declare common ancestor methods on `CodeElement`. This enables more uniform traversal up the tree of code elements and querying of ancestors.

The expression `e.ancestorOp().ancestorOp()` will first traverse up the code model tree from `e` to the nearest ancestor operation and then traverse again to the subsequent nearest ancestor operation, both skipping intermediate block and body elements. The first traversal can start from any kind of code element.

The methods `ancestor{Op, Body, Block}` are currently implemented as default methods. While the implementations are simple it is easy to make mistakes handling unbound or root operations. They can be more directly implemented in a follow on PR.

-------------

Commit messages:
 - Cleanup.
 - Doc updates.
 - Adjust isAncestorOf. Add test.
 - Update examples.
 - Merge remote-tracking branch 'upstream/code-reflection' into ancestor
 - Uses. Add CodeElement.isAncestorOf.
 - Unify ancestor functionality on CodeElement.

Changes: https://git.openjdk.org/babylon/pull/479/files
  Webrev: https://webrevs.openjdk.org/?repo=babylon&pr=479&range=00
  Stats: 518 lines in 32 files changed: 326 ins; 112 del; 80 mod
  Patch: https://git.openjdk.org/babylon/pull/479.diff
  Fetch: git fetch https://git.openjdk.org/babylon.git pull/479/head:pull/479

PR: https://git.openjdk.org/babylon/pull/479


More information about the babylon-dev mailing list