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>
Go to the source code of this file.
|
| 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.
|
Checked casts between node structs, iteration over lists of nodes, and walking the tree from C++.
◆ isa()
Whether the given node, which must not be null, is of the given struct.
- Template Parameters
-
| T | The node struct type to check against. |
- Parameters
-
- Returns
- True if the node is of the given struct, false otherwise.
◆ cast() [1/2]
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
-
| T | The node struct type to cast to. |
- Parameters
-
- 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
-
| T | The node struct type to cast to. |
- Parameters
-
- Returns
- The node cast to the given struct.
◆ dyn_cast() [1/2]
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
-
| T | The node struct type to cast to. |
- Parameters
-
- 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
-
| T | The node struct type to cast to. |
- Parameters
-
- Returns
- The node cast to the given struct if it is of that struct, or null otherwise.
◆ upcast() [1/2]
Return the pm_node_t embedded at the start of the given node struct.
- Template Parameters
-
- Parameters
-
| node | The 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
-
- Parameters
-
| node | The node struct to upcast. |
- Returns
- The pm_node_t embedded at the start of the node struct.
◆ visit_callback()
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
-
| F | The type of the visitor callable. |
- Parameters
-
| node | The current node being visited. |
| data | The 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
-
| F | The type of the visitor callable. |
- Parameters
-
| node | The root node to start the walk from. |
| visitor | The 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
-
| F | The type of the visitor callable. |
- Parameters
-
| node | The node whose children to walk. |
| visitor | The visitor callable to invoke for each child node. |
- Returns
- True if the walk completed, false if it was stopped.