Ruby 4.1.0dev (2026-10-21 revision 536dc6f98b72e6999a84bb6373562c43cb1ddf8a)
Data Structures | Functions
node.hpp File Reference

(536dc6f98b72e6999a84bb6373562c43cb1ddf8a)

Checked casts between node structs, iteration over lists of nodes, and walking the tree from C++. More...

#include "prism/compiler/cplusplus.hpp"
#include "prism/compiler/exceptions.hpp"
#include "prism/compiler/lifetimebound.hpp"
#include "prism/ast.hpp"
#include "prism/node.h"
#include "prism/node_type.hpp"
#include <cassert>
#include <cstddef>
#include <exception>
#include <functional>
#include <memory>
#include <type_traits>
Include dependency graph for node.hpp:

Go to the source code of this file.

Data Structures

class  prism::node_range
 The nodes in a list, usable in a range-based for loop. More...
 

Functions

template<typename T >
bool prism::isa (const pm_node_t *node) noexcept
 Whether the given node, which must not be null, is of the given struct.
 
template<typename T >
T * prism::cast (pm_node_t *node) noexcept
 Cast the given node, which must not be null and must be of the given struct, to that struct.
 
template<typename T >
const T * prism::cast (const pm_node_t *node) noexcept
 Cast the given node, which must not be null and must be of the given struct, to that struct.
 
template<typename T >
T * prism::dyn_cast (pm_node_t *node) noexcept
 Cast the given node to the given struct if it is of that struct, and return null otherwise.
 
template<typename T >
const T * prism::dyn_cast (const pm_node_t *node) noexcept
 Cast the given node to the given struct if it is of that struct, and return null otherwise.
 
template<typename T >
pm_node_t * prism::upcast (T *node) noexcept
 Return the pm_node_t embedded at the start of the given node struct.
 
template<typename T >
const pm_node_t * prism::upcast (const T *node) noexcept
 Return the pm_node_t embedded at the start of the given node struct.
 
template<typename F >
bool prism::visit_node (const pm_node_t *node, F &&visitor)
 Walk the tree rooted at the given node in pre-order, calling the visitor with each node.
 
template<typename F >
bool prism::visit_child_nodes (const pm_node_t *node, F &&visitor)
 Walk the children of the given node, as visit_node does for every node whose visitor returns PM_VISIT_DESCEND.
 

Detailed Description

Checked casts between node structs, iteration over lists of nodes, and walking the tree from C++.

Definition in file node.hpp.

Function Documentation

◆ cast() [1/2]

template<typename T >
const T * prism::cast ( const pm_node_t *  node)
noexcept

Cast the given node, which must not be null and must be of the given struct, to that struct.

The type is checked in debug builds.

Template Parameters
TThe node struct type to cast to.
Parameters
nodeThe node to cast.
Returns
The node cast to the given struct.

Definition at line 64 of file node.hpp.

◆ cast() [2/2]

template<typename T >
T * prism::cast ( pm_node_t *  node)
noexcept

Cast the given node, which must not be null and must be of the given struct, to that struct.

The type is checked in debug builds.

Template Parameters
TThe node struct type to cast to.
Parameters
nodeThe node to cast.
Returns
The node cast to the given struct.

Definition at line 50 of file node.hpp.

Referenced by prism::cast(), and prism::cast().

◆ dyn_cast() [1/2]

template<typename T >
const T * prism::dyn_cast ( const pm_node_t *  node)
noexcept

Cast the given node to the given struct if it is of that struct, and return null otherwise.

The node may be null, in which case this returns null.

Template Parameters
TThe node struct type to cast to.
Parameters
nodeThe node to cast.
Returns
The node cast to the given struct if it is of that struct, or null otherwise.

Definition at line 93 of file node.hpp.

◆ dyn_cast() [2/2]

template<typename T >
T * prism::dyn_cast ( pm_node_t *  node)
noexcept

Cast the given node to the given struct if it is of that struct, and return null otherwise.

The node may be null, in which case this returns null.

Template Parameters
TThe node struct type to cast to.
Parameters
nodeThe node to cast.
Returns
The node cast to the given struct if it is of that struct, or null otherwise.

Definition at line 79 of file node.hpp.

Referenced by prism::dyn_cast(), and prism::dyn_cast().

◆ isa()

template<typename T >
bool prism::isa ( const pm_node_t *  node)
noexcept

Whether the given node, which must not be null, is of the given struct.

Template Parameters
TThe node struct type to check against.
Parameters
nodeThe node to check.
Returns
True if the node is of the given struct, false otherwise.

Definition at line 35 of file node.hpp.

Referenced by prism::isa().

◆ upcast() [1/2]

template<typename T >
const pm_node_t * prism::upcast ( const T *  node)
noexcept

Return the pm_node_t embedded at the start of the given node struct.

Template Parameters
TThe node struct type.
Parameters
nodeThe node struct to upcast.
Returns
The pm_node_t embedded at the start of the node struct.

Definition at line 118 of file node.hpp.

◆ upcast() [2/2]

template<typename T >
pm_node_t * prism::upcast ( T *  node)
noexcept

Return the pm_node_t embedded at the start of the given node struct.

Template Parameters
TThe node struct type.
Parameters
nodeThe node struct to upcast.
Returns
The pm_node_t embedded at the start of the node struct.

Definition at line 105 of file node.hpp.

Referenced by prism::upcast(), and prism::upcast().

◆ visit_child_nodes()

template<typename F >
bool prism::visit_child_nodes ( const pm_node_t *  node,
F &&  visitor 
)

Walk the children of the given node, as visit_node does for every node whose visitor returns PM_VISIT_DESCEND.

Returns false if the walk was stopped. If the visitor throws, the walk stops and the exception propagates from this function.

Template Parameters
FThe type of the visitor callable.
Parameters
nodeThe node whose children to walk.
visitorThe visitor callable to invoke for each child node.
Returns
True if the walk completed, false if it was stopped.

Definition at line 258 of file node.hpp.

Referenced by prism::visit_child_nodes().

◆ visit_node()

template<typename F >
bool prism::visit_node ( const pm_node_t *  node,
F &&  visitor 
)

Walk the tree rooted at the given node in pre-order, calling the visitor with each node.

The visitor returns PM_VISIT_DESCEND to continue into that node's children, PM_VISIT_SKIP to continue past them, or PM_VISIT_STOP to end the walk. Returns false if the walk was stopped. If the visitor throws, the walk stops and the exception propagates from this function.

Template Parameters
FThe type of the visitor callable.
Parameters
nodeThe root node to start the walk from.
visitorThe visitor callable to invoke for each node.
Returns
True if the walk completed, false if it was stopped.

Definition at line 234 of file node.hpp.

Referenced by prism::visit_node().