10#include "prism/compiler/cplusplus.hpp"
11#include "prism/compiler/exceptions.hpp"
12#include "prism/compiler/lifetimebound.hpp"
34template <
typename T> [[nodiscard]]
bool
36 static_assert(
is_node_v<T>,
"isa<T> requires a node struct declared in ast.h");
37 assert(node !=
nullptr);
49template <
typename T> [[nodiscard]] T *
52 return reinterpret_cast<T *
>(node);
63template <
typename T> [[nodiscard]]
const T *
66 return reinterpret_cast<const T *
>(node);
78template <
typename T> [[nodiscard]] T *
80 return (node !=
nullptr &&
isa<T>(node)) ?
reinterpret_cast<T *
>(node) :
nullptr;
92template <
typename T> [[nodiscard]]
const T *
94 return (node !=
nullptr &&
isa<T>(node)) ?
reinterpret_cast<const T *
>(node) :
nullptr;
104template <
typename T> [[nodiscard]]
pm_node_t *
106 static_assert(
is_node_v<T>,
"upcast requires a node struct declared in ast.h");
117template <
typename T> [[nodiscard]]
const pm_node_t *
119 static_assert(
is_node_v<T>,
"upcast requires a node struct declared in ast.h");
148 pm_node_t *
const *
end() const noexcept {
return list_->nodes + list_->size; }
155 std::size_t
size() const noexcept {
return list_->size; }
162 [[nodiscard]]
bool empty() const noexcept {
return list_->size == 0; }
185#ifdef PRISM_HAS_EXCEPTIONS
187 std::exception_ptr exception;
207#ifdef PRISM_HAS_EXCEPTIONS
209 return std::invoke(*context->
callable, node);
211 context->exception = std::current_exception();
215 return std::invoke(*context->
callable, node);
233template <
typename F>
bool
235 static_assert(std::is_invocable_r_v<pm_visit_result_t, F &, const pm_node_t *>,
"visit_node requires a callable taking const pm_node_t * and returning pm_visit_result_t");
239#ifdef PRISM_HAS_EXCEPTIONS
240 if (context.exception) std::rethrow_exception(context.exception);
257template <
typename F>
bool
259 static_assert(std::is_invocable_r_v<pm_visit_result_t, F &, const pm_node_t *>,
"visit_child_nodes requires a callable taking const pm_node_t * and returning pm_visit_result_t");
263#ifdef PRISM_HAS_EXCEPTIONS
264 if (context.exception) std::rethrow_exception(context.exception);
struct pm_node_list pm_node_list_t
A list of nodes in the source, most often used for lists of children.
struct pm_node pm_node_t
This is the base structure that represents a node in the syntax tree.
The mapping between node structs and node types, generated from the same configuration as ast....
bool empty() const noexcept
Whether there are no nodes.
Definition node.hpp:162
pm_node_t *const * end() const noexcept
A pointer one past the last node.
Definition node.hpp:148
std::size_t size() const noexcept
The number of nodes.
Definition node.hpp:155
pm_node_t *const * begin() const noexcept
A pointer to the first node.
Definition node.hpp:141
node_range(const pm_node_list_t &list) noexcept
Create a range over the nodes in the given list.
Definition node.hpp:134
Functions related to nodes in the AST.
PRISM_EXPORTED_FUNCTION bool pm_visit_node(const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1)
Visit each of the nodes in this subtree using the given visitor callback.
pm_visit_result_t
The value returned by a visitor callback, which determines where the walk goes next.
Definition node.h:49
@ PM_VISIT_STOP
End the walk without visiting any more nodes.
Definition node.h:57
PRISM_EXPORTED_FUNCTION bool pm_visit_child_nodes(const pm_node_t *node, pm_visit_callback_t visitor, void *data) PRISM_NONNULL(1)
Visit the children of the given node with the given callback.
bool isa(const pm_node_t *node) noexcept
Whether the given node, which must not be null, is of the given struct.
Definition node.hpp:35
T * 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.
Definition node.hpp:79
pm_visit_result_t visit_callback(const pm_node_t *node, void *data) noexcept
Forwards the C visitor callback to a C++ callable.
Definition node.hpp:204
bool 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.
Definition node.hpp:234
bool 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...
Definition node.hpp:258
T * 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.
Definition node.hpp:50
pm_node_t * upcast(T *node) noexcept
Return the pm_node_t embedded at the start of the given node struct.
Definition node.hpp:105
The traits that map node structs to node types.
constexpr bool is_node_v
Whether the given type is one of the node structs declared in ast.h.
Definition node_type.hpp:34
constexpr pm_node_type_t node_type_v
The node type of the given node struct.
Definition node_type.hpp:25
The state shared between a walk and its C callback, passed through the C function's void * data param...
Definition node.hpp:174
visit_context(F *visitor) noexcept
The context for a visit, holding the visitor and any exception thrown.
Definition node.hpp:180
F * callable
The visitor that the walk calls for each node.
Definition node.hpp:183