Prism Ruby parser
Loading...
Searching...
No Matches
node.hpp File Reference

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...
struct  prism::detail::visit_context< F >
 The state shared between a walk and its C callback, passed through the C function's void * data parameter. 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>
pm_visit_result_t prism::detail::visit_callback (const pm_node_t *node, void *data) noexcept
 Forwards the C visitor callback to a C++ callable.
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++.

Function Documentation

◆ isa()

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

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.

◆ cast() [1/2]

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

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.

◆ cast() [2/2]

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

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.

◆ dyn_cast() [1/2]

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

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.

◆ dyn_cast() [2/2]

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

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.

◆ upcast() [1/2]

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

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.

◆ upcast() [2/2]

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

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.

◆ visit_callback()

template<typename F>
pm_visit_result_t prism::detail::visit_callback ( const pm_node_t * node,
void * data )
noexcept

Forwards the C visitor callback to a C++ callable.

An exception cannot unwind through the C frames that call this function, so one thrown by the callable is caught here and the walk is stopped. The function that started the walk rethrows it once the C walk has returned. Without exceptions, the callable cannot throw, so it is called directly.

Template Parameters
FThe type of the visitor callable.
Parameters
nodeThe current node being visited.
dataThe visit context containing the visitor and exception state.
Returns
The result of the visit, indicating whether to descend, skip, or stop.

◆ 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.

◆ 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.